VBA Clear Range in Excel will Clear a specific range or entire worksheet using Clear method of Range Object. ‘Rang.Clear’ method will clear the range including the formats like border, font styles, background cell and set to default.
Excel VBA to Clear a Range – Syntax
Here is the syntax to clear a range. You can clear the data in any range including formats using VBA ‘Range.Clear’ method.
Excel VBA to Clear a Range – Examples
The below macro will show you how to clear a range using VBA. In this example, we are clearing range “A2 to D10” using VBA.
Sub VBA_Clear_Range() Range("A2:D10").Clear End Sub
Excel VBA to Clear a Range – Instructions
Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:
- Open an Excel Workbook from your start menu or type Excel in your run command
- Enter some data in any cells in range “A10 to D10” to test this macro.
- Press Alt+F11 to Open VBA Editor or you can goto Developer Table from Excel Ribbon and click on the Visual Basic Command to launch the VBA Editor
- Insert a Module from Insert Menu of VBA
- Copy the above code (for clearing the range using VBA) and Paste in the code window(VBA Editor)
- Save the file as Macro Enabled Workbook (i.e; .xlsm file format)
- Press ‘F5′ to run it or Keep Pressing ‘F8′ to debug the code line by line.
Now you can observe that the data in range “A2 to D10” is clear. You can apply any formats and background color, and try this macro. This will clear everything and set to the default.
If you want to clear only the data you can use Range.ClearContents method, to clear the comments in a range you use the Range.ClearComments method.
Range.Clear: Will clear everything including cell formats.
Range.ClearContents: Will clear only the content/data of the range, formats will remain same.
Range.ClearComments: Will clear only the comments, formats and data will remain same.