In this VBA Tutorial, you learn how to clear cells (including clearing cells totally, their format but not their contents, their contents but not their format, and other similar combinations) with macros.
This VBA Tutorial is accompanied by Excel workbooks containing the macros I use in the examples below. You can get immediate access to these example workbooks by subscribing to the Power Spreadsheets Newsletter.
Use the following Table of Contents to navigate to the section you're interested in.
Table of Contents
Related VBA and Macro Tutorials
The following VBA and Macro Tutorials may help you better understand and implement the contents below:
- General VBA constructs and structures:
- Learn about important VBA constructs here.
- Learn how to work with the Visual Basic Editor here.
- Learn how to work with Excel Sub procedures here.
- Learn about the Excel Object Model, and how to create object references, here.
- Learn about the Range object, and how to refer to cells, here.
- Learn how to work with properties here.
- Learn how to work with methods here.
- Learn how to declare and work with variables here.
- Learn about data types here.
- Learn how to work with loops here.
- Learn about important VBA constructs here.
- Practical VBA applications and macro examples:
You can find additional VBA and Macro Tutorials in the Archives.
#1: Clear Cell
VBA Code to Clear Cell
To clear cells using VBA, use a statement with the following structure:
Cells.Clear
Process Followed by VBA to Clear Cell
VBA Statement Explanation
- Item: Cells.
- VBA Construct: Range object.
- Description: Range object representing the cells you want to clear.
You can usually return a Range object with constructs such as the Worksheet.Range, Worksheet.Cells (with Range.Item), Range.Offset, Range.Resize or Application.ActiveCell properties. If you explicitly declare an object variable to represent Cells, use the Range object data type.
- VBA Construct: Range object.
- Item: Clear.
- VBA Construct: Range.Clear method.
- Description: The Range.Clear method clears the Range object you specify (Cells). Range.Clear clears the entire Range object, including values, formulas and formatting.
- VBA Construct: Range.Clear method.
Macro Example to Clear Cell
The following macro example clears cells A5 to C9 (myRange) in the worksheet named “Clear Cell” of the workbook containing the macro (ThisWorkbook).
Sub clearCell() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-clear-cell/ 'declare object variable to hold reference to cells to clear Dim myRange As Range 'identify cells to clear Set myRange = ThisWorkbook.Worksheets("Clear Cell").Range("A5:C9") 'clear cells (including formatting) myRange.Clear End Sub
Effects of Executing Macro Example to Clear Cell
The following images illustrate the results of executing the macro example.
- Before macro execution: Cells A5 to C9 contain the string “data”, have a light blue fill, and the font is formatted as bold.
- After macro execution: Cells A5 to C9 (including both data and formatting) are cleared.
#2: Clear Cell Contents and Keep Formatting
VBA Code to Clear Cell Contents and Keep Formatting
To clear cell contents (but not formatting) using VBA, use a statement with the following structure:
Cells.ClearContents
Process Followed by VBA to Clear Cell Contents and Keep Formatting
VBA Statement Explanation
- Item: Cells.
- VBA Construct: Range object.
- Description: Range object representing the cells where you want to clear the contents but not the formatting.
You can usually return a Range object with constructs such as the Worksheet.Range, Worksheet.Cells (with Range.Item), Range.Offset, Range.Resize or Application.ActiveCell properties. If you explicitly declare an object variable to represent Cells, use the Range object data type.
- VBA Construct: Range object.
- Item: ClearContents.
- VBA Construct: Range.ClearContents method.
- Description: The Range.ClearContents method clears values and formulas from the Range object you specify (Cells). Range.ClearContents leaves formatting intact.
- VBA Construct: Range.ClearContents method.
Macro Example to Clear Cell Contents and Keep Formatting
The following macro example clears the contents (but not the formatting) of cells A10 to C14 (myRange) in the worksheet named “Clear Cell” of the workbook containing the macro (ThisWorkbook).
Sub clearCellContentsKeepFormatting() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-clear-cell/ 'declare object variable to hold reference to cells to clear contents but not formatting Dim myRange As Range 'identify cells to clear contents and keep formatting Set myRange = ThisWorkbook.Worksheets("Clear Cell").Range("A10:C14") 'clear cell contents (but not formatting) myRange.ClearContents End Sub
Effects of Executing Macro Example to Clear Cell Contents and Keep Formatting
The following images illustrate the results of executing the macro example.
- Before macro execution: Cells A10 to C14 contain the string “data”, have a light gold fill, and the font is formatted as bold.
- After macro execution: Cell contents of cells A10 to C14 are cleared. The formatting is kept.
#3: Clear Cell Formatting
VBA Code to Clear Cell Formatting
To clear cell formatting using VBA, use a statement with the following structure:
Cells.ClearFormats
Process Followed by VBA to Clear Cell Formatting
VBA Statement Explanation
- Item: Cells.
- VBA Construct: Range object.
- Description: Range object representing the cells where you want to clear cell formatting.
You can usually return a Range object with constructs such as the Worksheet.Range, Worksheet.Cells (with Range.Item), Range.Offset, Range.Resize or Application.ActiveCell properties. If you explicitly declare an object variable to represent Cells, use the Range object data type.
- VBA Construct: Range object.
- Item: ClearFormats.
- VBA Construct: Range.ClearFormats method.
- Description: The Range.ClearFormats method clears the formatting of the Range object you specify (Cells). Range.ClearFormats doesn't clear values or formulas.
- VBA Construct: Range.ClearFormats method.
Macro Example to Clear Cell Formatting
The following macro clears the cell formatting of cells A15 to C19 (myRange) of the worksheet named “Clear Cell” in the workbook containing the macro (ThisWorkbook).
Sub clearCellFormatting() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-clear-cell/ 'declare object variable to hold reference to cells to clear formatting Dim myRange As Range 'identify cells to clear formatting Set myRange = ThisWorkbook.Worksheets("Clear Cell").Range("A15:C19") 'clear cell formatting myRange.ClearFormats End Sub
Effects of Executing Macro Example to Clear Cell Formatting
The following images illustrate the results of executing the macro example.
- Before macro execution: Cells A15 to C19 contain the string “data”, have a light green fill, and the font is formatted as bold.
- After macro execution: The formatting of cells A15 to C19 is cleared.
#4: Clear Cell Color
VBA Code to Clear Cell Color
To clear cell color using VBA, use a statement with the following structure:
Cells.Interior.Color = xlColorIndexNone
Process Followed by VBA to Clear Cell Color
VBA Statement Explanation
- Item: Cells.
- VBA Construct: Range object.
- Description: Range object representing the cells where you want to clear cell formatting.
You can usually return a Range object with constructs such as the Worksheet.Range, Worksheet.Cells (with Range.Item), Range.Offset, Range.Resize or Application.ActiveCell properties. If you explicitly declare an object variable to represent Cells, use the Range object data type.
- VBA Construct: Range object.
- Item: Interior.
- VBA Construct: Range.Interior property and Interior object.
- Description: The Range. Interior property returns an Interior object representing the interior of the cell range you specify (Cells).
- VBA Construct: Range.Interior property and Interior object.
- Item: Color.
- VBA Construct: Interior.Color property.
- Description: The Interior.Color property allows you to set the primary color of the cell interior represented by the Interior object returned by Range.Interior.
- VBA Construct: Interior.Color property.
- Item: =.
- VBA Construct: Assignment operator.
- Description: The assignment operator assigns the xlColorIndexNone value to the Interior.Color property.
- VBA Construct: Assignment operator.
- Item: xlColorIndexNone.
- VBA Construct: xlColorIndexNone constant.
- Description: The xlColorIndexNone constant specifies that the color of the Interior object representing the interior of Cells is none.
- VBA Construct: xlColorIndexNone constant.
Macro Example to Clear Cell Color
The following macro clears the cell color of cells A20 to C24 (myRange) in the worksheet named “Clear Cell” of the workbook containing the macro (ThisWorkbook).
Sub clearCellColor() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-clear-cell/ 'declare object variable to hold reference to cells to clear cell color Dim myRange As Range 'identify cells to clear cell color Set myRange = ThisWorkbook.Worksheets("Clear Cell").Range("A20:C24") 'clear cell color myRange.Interior.Color = xlColorIndexNone End Sub
Effects of Executing Macro Example to Clear Cell Color
The following images illustrate the results of executing the macro example.
- Before macro execution: Cells A20 to C24 contain the string “data”, have a light orange fill, and the font is formatted as bold.
- After macro execution: The fill color of cells A20 to C24 is cleared.
#5: Clear Cells with Zero
VBA Code to Clear Cells with Zero
To clear cells with zero within a cell range using VBA, use a macro with the following statement structure:
For Each Cell In Range If Cell.Value = myValue Then Cell.Clear Next Cell
Process Followed by VBA to Clear Cells with Zero
VBA Statement Explanation
Lines #1 and #3: For Each Cell In Range | Next Cell
- Item: For Each… In… Next.
- VBA Construct: For Each… Next statement.
- Description: The For Each… Next statement repeats the statement within the loop (line #2) for each element (Cell) in the cell range (Range) you want to search for zeroes in.
- VBA Construct: For Each… Next statement.
- Item: Cell.
- VBA Construct: Element of the For Each… Next statement and object variable of the Range object data type.
- Description: The Element of the For Each… Next statement is an object variable used to iterate through the elements (Cell) of the cell range (Range) you want to search for zeroes in.
If you explicitly declare an object variable to represent Cell, use the Range object data type.
- VBA Construct: Element of the For Each… Next statement and object variable of the Range object data type.
- Item: Range.
- VBA Construct: Group of the For Each… Next statement and Range object.
- Description: The For Each… Next statement repeats the statements within the loop (line #2) for each element (Cell) in the Group (Range). Range is a Range object representing the cells where you want to search for zeroes.
You can usually return a Range object with constructs such as the Worksheet.Range, Worksheet.Cells (with Range.Item), Range.Offset, Range.Resize or Application.ActiveCell properties. If you explicitly declare an object variable to represent Range, use the Range object data type.
- VBA Construct: Group of the For Each… Next statement and Range object.
Line #2: If Cell.Value = myValue Then Cell.Clear
- Item: If… Then.
- VBA Construct: If… Then… Else statement.
- Description: The If… Then… Else statement conditionally executes a group of statements depending on the value of an expression. For these purposes:
- The If… Then… Else statement tests the specified condition (Cell.Value = myValue).
- If the condition is met and returns True: Cell.Clear is executed.
- If the condition is not met and returns False: Execution continues with the statement following the If… Then… Else statement (Next Cell).
- The If… Then… Else statement tests the specified condition (Cell.Value = myValue).
- VBA Construct: If… Then… Else statement.
- Item: Cell.
- VBA Construct: Object variable of the Range object data type.
- Description: Cell is an object variable used to iterate through the elements of the cell range (Range) you want to search for zeroes in. Within the If… Then… Else statement, Cell represents the individual cell the For Each… Next loop is currently iterating through.
If you explicitly declare an object variable to represent Cell, use the Range object data type.
- VBA Construct: Object variable of the Range object data type.
- Item: Value.
- VBA Construct: Range.Value property.
- Description: The Range.Value property returns the value in the cell the For Each…Next loop is currently iterating through.
- VBA Construct: Range.Value property.
- Item: myValue.
- VBA Construct: Variable of a numeric data type.
- Description: myValue represents the value you want to search for and use to determine which cells to clear. Within the macro structure used in this VBA Tutorial, myValue is 0 (zero).
If you explicitly declare a variable to represent myValue, use a numeric data type such as Long, Single or Double (depending on the value you're searching for).
- VBA Construct: Variable of a numeric data type.
- Item: Cell.Value = myValue.
- VBA Construct: Condition of If… Then… Else statement.
- Description: This condition is an expression that evaluates to True or False. Cell.Value = myValue returns True or False, as follows:
- True: Value of Cell is equal to myValue (zero).
- False: Value of Cell isn't equal to myValue (zero).
- True: Value of Cell is equal to myValue (zero).
- VBA Construct: Condition of If… Then… Else statement.
- Item: Clear.
- VBA Construct: Range.Clear method.
- Description: The Range.Clear method clears the cell the For Each… Next loop is currently iterating through. Range.Clear clears the entire Range object, including values, formulas and formatting.
If you don't want to clear the entire Range object, but only its contents, formatting or cell color, please refer to the appropriate sections above.
- VBA Construct: Range.Clear method.
Macro Example to Clear Cells with Zero
The following macro example clears the cells with zero (0) in cells A25 to C29 (myRange) in the worksheet named “Clear Cell” of the workbook containing the macro (ThisWorkbook).
Sub clearCellsWithZero() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-clear-cell/ 'declare object variables to hold references to cell range where you search for zeroes Dim myRange As Range 'declare object variable used to iterate through the elements of the cell range Dim iCell As Range 'declare variable to hold value (zero) you search for Dim myValue As Long 'identify cells to search for zeroes Set myRange = ThisWorkbook.Worksheets("Clear Cell").Range("A25:C29") 'set value (zero) to search for myValue = 0 'loop through each cell (iCell) of the cell range (myRange) For Each iCell In myRange 'test if value is zero. If condition is met, clear cell If iCell.Value = myValue Then iCell.Clear Next iCell End Sub
Effects of Executing Macro Example to Clear Cells with Zero
The following images illustrate the results of executing the macro example.
- Before macro execution: Cells A25 to C29 contain the string “data” or the value zero (0), have a light gray fill, and the font is formatted as bold.
- After macro execution: Cells between A25 to C29 containing a zero (0) are cleared (including both data and formatting).
References to VBA Constructs Used in this VBA Tutorial
Use the following links to visit the appropriate webpage in the Microsoft Developer Network:
- Identify the workbook and worksheet where the cells to clear are located:
- Identify the cells to clear:
- Range object.
- Worksheet.Range property.
- Worksheet.Cells property.
- Application.ActiveCell property.
- Application.Selection property.
- Range.Range property.
- Range.Cells property.
- Range.Item property.
- Range.Offset property.
- Range.Resize property.
- For Each… Next statement.
- If… Then… Else statement.
- Range.Value property.
- Range object.
- Clear cells:
- Work with variables and data types:
- Dim statement.
- Set statement.
- = operator.
- Data types:
- Dim statement.