Excel VBA to Clear Contents of Range (3 Suitable Cases)

Consider a dataset where you may want to reuse existing cells. You can use VBA code to clear the contents of an Excel file. Here’s an overview of our dataset, with sales records in several cities and countries.

Clear Contents of Range with Excel VBA


Case 1 – Clear All Contents of aRange

Part 1 – Clear the Contents of a Specific Range of Cells

Steps:

  • Go to the top ribbon and in the Developer tab, then press on the Visual Basic option from the menu.
  • You can use Alt + F11 to open the Microsoft Visual Basic for Applications window if you don’t have the Developer tab added.

Microsoft Visual Basic for Applications

  • Now, a window named Microsoft Visual Basic for Applications will appear. From the top menu bar, press on Insert and select the Module option.

insert module

  • A new Module window will appear. Paste this VBA code into the box.

Using Clear Command:

Sub Clear_Contents_Range()
Range("B4:D5").Clear
End Sub

VBA Code Using Clear Command

  • Click on the Run option from the ribbon and select Run Sub/UserForm, or press F5 to run the code.

Run VBA Code

  • The cells will be cleared and the formatting will be removed, but the empty cells will be there.

Using Contents of Range Using VBA


Using Delete Command:

You can also use the Delete command instead of the Clear command. Use the following code into the module.

Sub Delete_Contents_Range()
Range("B4:D5").Delete
End Sub

Using Delete Command

The selected range of cells will be removed completely in this case.

Using Delete Command

Read More: Excel VBA: Clear Contents If Cell Contains Specific Values


Difference Between Clear and Delete Command in Excel VBA:

The key difference between the Clear and Delete commands in Excel VBA is that the Delete command removes the selected range of cells completely while the Clear command removes only the cell values and the formatting.


Part 2 – Clear Contents of Full Worksheet

  • Use the following code:
Sub Delete_Contents_Range()
Worksheets("1.2").Cells.Clear
End Sub

Clear Contents of Full Worksheet

You can use the Delete command to remove all used cells in a worksheet.

  • Use the code given below:
Sub Delete_Contents_Range()
Worksheets("1.2").Cells.Delete
End Sub

Part 3 – Clear the Contents of the Active Worksheet

  • Use the following code:
Sub Delete_Contents_Range()
ActiveSheet.Cells.Clear
End Sub

Clear Contents of Active Worksheet

  • You can also use the delete command to remove the cells completely from the active worksheet: module.

Sub Delete_Contents_Range()
ActiveSheet.Cells.Delete
End Sub

Read More: How to Clear Contents of a Sheet with Excel VBA


Case 2 – Clear Contents of a Range but Keeping the Formatting

Part 1 – Contents of a Specific Range

  • Use the following code and change your range if needed.
Sub Delete_cell_Keeping_format()
Range("B2:D4").ClearContents
End Sub

VBA Code of Clear Contents of Range Keeping the Formatting

  • When you run the code, you will see the selected cells are cleared, but the formatting remains.

Output of Clear Contents of Range Keeping the Formatting


Part 2 – Contents of a Specific Worksheet

  • Use the following code and change the worksheet and range as necessary:
Sub Delete_Worksheet_Cells_Keeping_format()
Worksheets("2.2").Range("B2:D4").ClearContents
End Sub

VBA Code of Clearing Contents of Specific Worksheet

VBA Code Explanation:

  • Worksheets(“2.2”) calls the worksheet named “2.2”. If you want to call a worksheet of any other name then, insert the name of the worksheet in the inverted commas.
  • Range(“B2:D4”) is specifying the range of cells that will be cleared. You can edit the cell range as to your need.

Part 3 – Contents from Other Workbooks

  • You have to keep that workbook open.
  • Use the following code:
Sub Delete_Other_Workbook_Cells_Keeping_format()
Workbooks("file 1").Worksheets("Sheet1").Range("B3:D12").ClearContents
End Sub

VBA Code of Clearing Contents from Other Workbook

VBA Code Explanation:

  • Workbooks(“file 1”) calls the workbook named “File 1”. While using your workbooks, you have to insert the name of the workbook in the inverted commas.
  • Worksheets(“Sheet1”) calls the worksheet named “Sheet 1”. To call any other worksheet, just insert the name of the worksheet instead of “Sheet 1” in this code.
  • Range(“B3:D13”) is specifying the range of cells that will be cleared. You can edit the cell range as your need.

Case 3 – Clear Contents of Specific Range of All Worksheets at Once

  • Paste this code into the module to clear the contents of range B2:D4 of all worksheets. If you want to change the cell range, edit the 4th line and insert your data range.
Sub Clear_Specific_Range_All_Worksheets()
Dim W_S As Worksheet
For Each W_S In ActiveWorkbook.Worksheets
W_S.Range("B2:D4").ClearContents
Next W_S
End Sub

VBA Code of Clearing Contents of Specific Range of All Worksheets at Once


Things to Remember

  • Using the Clear command will only clear the cell values and formats.
  • Delete command will remove the cells completely.
  • The ClearContents command will clear the values of the cells only and keep the cell formats untouched.

Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Osman Goni Ridwan
Osman Goni Ridwan

OSMAN GONI RIDWAN is a marine engineer who loves exploring Excel and VBA programming. Programming, to him, is a time-saving tool for efficiently managing data, files, and internet tasks. He is proficient in Rhino3D, Maxsurf, C++, MS Office, AutoCAD, Excel, and VBA and goes beyond the basics. Armed with a B.Sc in Naval Architecture & Marine Engineering from BUET, OSMAN has transitioned into the role of a content developer. Specializing in creating technical content exclusively centered around Excel... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo