萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> Excel 教學 >> Excel 綜合

Excel VBA Range.Clear 系列方法:清除內容、格式、批註、全部的差異

VBA 提供 4 種 Range.Clear* 方法,針對不同清除需求精準操作,避免誤刪格式或批註。

四大方法對照表

方法 清除內容 清除格式 清除批註 適用情境
ClearContents ✓ ✗ ✗ 保留格式/批註,僅清資料(如每月重填報表)
ClearFormats ✗ ✓ ✗ 重設樣式、移除條件格式
ClearComments ✗ ✗ ✓ 批量移除舊批註
Clear ✓ ✓ ✓ 完全重置儲存格狀態

程式碼範例(以第 9 個工作表 B1:F8 為例)

1. 僅清除內容

Sub testClearContents()
    Worksheets(9).Range("B1:F8").ClearContents
End Sub

2. 僅清除格式

Sub testClearFormats()
    Worksheets(9).Range("B1:F8").ClearFormats
End Sub

3. 僅清除批註

Sub testClearComments()
    Worksheets(9).Range("B1:F8").ClearComments
End Sub

4. 徹底清除(內容+格式+批註)

Sub testClear()
    Worksheets(9).Range("B1:F8").Clear
End Sub

補充:ClearNotes (Excel 365/2019+)

新版 Excel 區分「批註」與「便箋」,ClearComments 僅清除舊式批註,新式便箋需用 ClearNotes 或 Clear。

最佳實務

  • 清楚知道要保留什麼,選對應的 Clear* 方法。
  • 大範圍操作前先設 Application.ScreenUpdating = False 加速。
  • 搭配 SpecialCells(xlCellTypeConstants) 僅清除常數值,保留公式。
copyright © 萬盛學電腦網 all rights reserved