How to Clear Cells in Excel VBA (9 Easy Methods)

The following dataset has the Product and Sales columns. We will clear some cells with VBA.

How to Clear Cells in Excel VBA


Method 1 – Using the VBA Clear Command in Excel to Clear a Single Cell

We will clear cell C9 using the Clear method.

Using Clear Methods in Excel VBA

Step 1 – Inserting a Module

  • Go to the Developer tab and select Visual Basic.

This will bring out the VBA editor window. You can press Alt + F11 to bring out the VBA editor window.

  • From the Insert tab, select Module.

Here’s the Module.

Step 2 – Using Code to Clear Cells in Excel

  • Insert the following code in the Module.
Sub clear_Single_cell()
    Range("C9").Clear
End Sub

VBA code to Clear Single Cell in Excel

Code Breakdown

  • We declared clear_single_cell as our Sub.
  • The Clear method is used to clear cell C9.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • Go to the Developer tab and select Macros.

A Macro dialog box will appear. You can press Alt + F8 to bring out the Macro dialog box.

  • Select clear_Single_cell from Macro name.
  • Click on Run.

This clears cell C9 as per the code.

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


Method 2 – Clearing Range of Cells with Excel VBA

We will use the Clear method in VBA to clear cells B7:C9, which are marked with green color.

Clearing Range of Cells in Excel VBA

Steps:

  • Followed Step 1 of Method 1 to get a Module.
  • Insert the following code in the Module.
Sub clear_Range_of_cell()
     Range("B7:C9").Clear
End Sub

Code Breakdown

  • We declare clear_Range_of_cell as our Sub.
  • The Clear method is used to clear cells B7:C9.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • Go to the Developer tab and select Macros.

A Macro dialog box will appear. You can press Alt + F8 to bring out the Macro dialog box.

  • Select clear_Range_of_cells from Macro name.
  • Click on Run.

You can see cells B7:C9 have been cleared in Excel VBA.

Range of Cell Cleared in Excel VBA

Read More: Excel VBA to Clear Contents of Range


Method 3 – Clearing Cells from the Selection in Excel

We will use the Clear method in VBA to clear selected cells.

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub Clear_Cells_from_Selection()
     Selection.Clear
End Sub

VBA Code to Clear Cells from Selection

Code Breakdown

  • We declared Clear_Cells_from_Selection as our Sub.
  • The Clear method is used to clear the selected cells.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • Select the cells you want to clear. We selected cells B7:C9.
  • Go to the Developer tab and select Macros.

A Macro dialog box will appear. You can press Alt + F8 to bring out the Macro dialog box.

  • Select Clear_Cells_from_Selection from Macro name.
  • Click on Run.

selecting Clear cells from selection from Macro Name to run code in Excel VBA

You can see the selected cells B7:C9 have been cleared in Excel VBA.

Read More: How to Clear Cells with Certain Value in Excel


Method 4 – Applying the VBA ClearContents Command to Clear Cell Data in Excel

We will use the ClearContents method in VBA to clear the contents in cell B7:C9.

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_Content_of_cells()
     Range("B7:C9").ClearContents
End Sub

Applying Clear Contents Method in VBA to Clear Cell Content

Code Breakdown

  • We declare clear_Content_of_cells as our Sub.
  • The ClearContents method is used to clear the cell content of cells B7:C9.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • To Run the code, go to the Developer tab and select Macros.

A Macro dialog box will appear. You can press Alt + F8 to bring out the Macro dialog box.

  • Select clear_Content_of_cells from Macro name.
  • Click on Run.

You can see the contents of cells B7:C9 have been cleared in Excel VBA. However, the format in the cells is still present.

Read More: How to Clear Contents Without Deleting Formulas Using VBA in Excel


Method 5 – Using the VBA ClearFormats Statement to Clear Cell Formatting

We will use the ClearFormats method in VBA to clear the formatting of the cells B5:C9.

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_Formates_of_cells()
     Range("B5:C9").ClearFormats
End Sub

Use of Clear Formats Method in Excel VBA to Clear Formats of Cells

Code Breakdown

  • We declare clear_Formates_of_cells as our Sub.
  • The ClearFormats method is used to clear the Formats of cells B5:C9.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • To Run the code, go to the Developer tab and select Macros.

A Macro dialog box will appear. You can press Alt + F8 to bring out the Macro dialog box.

  • Select Clear_Formates_of_cells from Macro name.
  • Click on Run.

Here’s the result.

Read More: How to Clear Contents in Excel Without Deleting Formatting


Method 6 – Using VBA to Clear Comments of Cells in Excel

You can see two comments in cells C8 and C8 respectively. We will use the ClearComments method in VBA to clear the comments of the cells.

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_Comments_from_cells()
    Range("B5:C9").ClearComments
End Sub

Use of Clear Comments in VBA to Clear Comments

Code Breakdown

  • We declare clear_Comments_from_cells as our Sub.
  • The ClearComments method is used to clear the Comments of cells B5:C9.
  • Save the code and close the VBA editor window.
  • Go back to the worksheet.
  • Run the code by going to Macros or pressing Alt + F8.

  • Select clear_Comments_from_cells from Macro name.
  • Click on Run.

There are no comments in the dataset.

Read More: How to Clear Cell Contents Based on Condition in Excel


Method 7 – Inserting the VBA ClearHyperlinks Command to Clear Hyperlinks from Cells

Cells B5:B9 contains Sheet Names. All these Sheet Names have Hyperlinks. We will use the ClearHyperlinks method in VBA to clear Hyperlinks from cells.

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_Hyperlinks_from_cells()
     Range("B5:B9").ClearHyperlinks
End Sub

Inserting Clear Hyperlinks in Excel VBA to Clear Cell Hyperlinks

Code Breakdown

  • We declare clear_Hyperlinks_from_cells as our Sub.
  • The ClearHyperlinks method is used to clear the Hyperlinks of cells B5:B9.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • To Run the code, go to the Developer tab and select Macros or press Alt + F8.

  • Select Clear_Hyperlinks_from_cells from Macro name.
  • Click on Run.

  • The hyperlinks no longer work.

Cleared Hyperlinks in Cell by Employing Excel VBA


Method 8 – Clearing Cell Color with Excel VBA

Cells B5:C9 have a green color. We will clear cell color in Excel VBA.

Cell Color Clearing by Use of Excel VBA

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_Color_from_cells()

     Dim cell_range As Range
     Set cell_range = Range("B5:C9")
     cell_range.Interior.Color = xlColorIndexNone

End Sub

Excel VBA to Clear Color of Cells

Code Breakdown

  • We declare clear_Color_from_cells as our Sub.
  • We take cell_range as Range.
  • We set the interior color of cells B5:C9 as xlColorIndexNone.
  • Save the code and close the VBA editor window.
  • Go back to the worksheet.
  • To Run the code, go to the Developer tab and select Macros or press Alt + F8.

A Macro dialog box will appear.

  • Select Clear_Color_from_cells from Macro name.
  • Click on Run.

You can see there is no color in the cells B5:C9 of the dataset.


Method 9 – Use VBA to Clear All Cells from theWorksheet in Excel

We will use the Clear method in VBA to clear all cells from the worksheet.

Clearing All Cells by Inserting Excel VBA

Steps:

  • Follow Step 1 of Method 1 to bring out the Module.
  • Insert the following code in the Module.
Sub clear_all_cells_from_worksheet()
     Worksheets("Clear All Cells ").Cells.Clear
End Sub

Using VBA to Clear All Cells in Excel Workbook

Code Breakdown

  • We declare clear_all_cells_from_worksheet as our Sub.
  • The Clear method is used to clear all the cells of the Clear All Cells sheet.
  • Save the code and close the VBA editor window.
  • Go back to the workbook.
  • To Run the code, go to the Developer tab and select Macros or press Alt + F8.

A Macro dialog box will appear.

  • Select Clear_all_cells_from_worksheet from Macro name.
  • Click on Run.

You’ll get an empty sheet.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Afia Kona
Afia Kona

Afia Aziz Kona, a graduate of NAME from Bangladesh University of Engineering & Technology, Bangladesh, serves as a technical content creator in the ExcelDemy project. Possessing a passion for innovation and critical thinking, she actively embraces challenges. Beyond her engineering background, Afia exhibits a keen interest in Excel, having authored numerous articles on Excel & VBA-related issues to simplify the experience for users facing obstacles in Excel. Apart from creating Excel tutorials, she is also interested... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo