VBA 提供 4 種 Range.Clear* 方法,針對不同清除需求精準操作,避免誤刪格式或批註。
| 方法 | 清除內容 | 清除格式 | 清除批註 | 適用情境 |
|---|---|---|---|---|
ClearContents |
✓ | ✗ | ✗ | 保留格式/批註,僅清資料(如每月重填報表) |
ClearFormats |
✗ | ✓ | ✗ | 重設樣式、移除條件格式 |
ClearComments |
✗ | ✗ | ✓ | 批量移除舊批註 |
Clear |
✓ | ✓ | ✓ | 完全重置儲存格狀態 |
Sub testClearContents()
Worksheets(9).Range("B1:F8").ClearContents
End Sub
Sub testClearFormats()
Worksheets(9).Range("B1:F8").ClearFormats
End Sub
Sub testClearComments()
Worksheets(9).Range("B1:F8").ClearComments
End Sub
Sub testClear()
Worksheets(9).Range("B1:F8").Clear
End Sub
新版 Excel 區分「批註」與「便箋」,ClearComments 僅清除舊式批註,新式便箋需用 ClearNotes 或 Clear。
Application.ScreenUpdating = False 加速。SpecialCells(xlCellTypeConstants) 僅清除常數值,保留公式。