Home / VBA / VBA ClearContents (from a Cell, Range, or Entire Worksheet)
Written by Puneet for Excel 2007, Excel 2010, Excel 2013, Excel 2016, Excel 2019, Excel for Mac
Key Points
- To clear content from a cell, you need to use the ClearContents method.
- You can use it for a single cell, a range, or an entire worksheet.
ClearContents Method
In VBA, there is a method called ClearContents that you can use to clear values and formulas from a cell, range of cells, and the entire worksheet.
Expression.ClearContents
To use this method, first, you need to define the expression somewhere you want to clear the content, and then type “.ClearContents”.
Clear Contents from a Single Cell
Let’s say you want to clear values from cell A1. First, you need to define cell A1 using the range object and then the ClearContents method.
So the code would be:
Sub ClearContentExamples()
Range("A1").ClearContents
End Sub
You can also use the cells property to define a cell and the code would be:
Sub ClearContentExamples()
Cells(1, 1).ClearContents
End Sub
Clear Contents from a Range
In the same way, you can define a range and then use the ClearContent method to clear values and formulas from that range. Let’s say you want to clear values from the range A1:A10, in this case, the code would be something like the below.
Sub ClearContentExamples()
Range("A1:A10").ClearContents
End Sub
Clear Content from an Entire Worksheet
To clear content from an entire worksheet, you can use the below code:
Sub ClearContentExamples()
Cells.ClearContents
End Sub
And to clear from a specific worksheet:
Worksheets("Sheet1").Cells.ClearContents
Clear Content from the Selection
If you want to write a dynamic code that can clear contents from the selected cells, then you can use the below code.
Sub ClearContentExamples()
Selection.ClearContents
End Sub
Other Methods
Below are some of the other methods that you can use:
Sub ClearContentExamples()
Range(“A1”).Clear ‘Clears Everything
Range(“A1”).ClearComments ‘Clear Comments
Range(“A1”).ClearFormats ‘Clears Formatting
Range(“A1”).ClearHyperlinks ‘Clear Hyperlinks
Range(“A1”).ClearNotes ‘Clear Notes
Range(“A1”).ClearOutline ‘Clears Outline
End Sub
More Tutorials
- Count Rows using VBA in Excel
- Excel VBA Font (Color, Size, Type, and Bold)
- Excel VBA Hide and Unhide a Column or a Row
- Excel VBA Range – Working with Range and Cells in VBA
- Apply Borders on a Cell using VBA in Excel
- Find Last Row, Column, and Cell using VBA in Excel
- Insert a Row using VBA in Excel
- Merge Cells in Excel using a VBA Code
- Select a Range/Cell using VBA in Excel
- SELECT ALL the Cells in a Worksheet using a VBA Code
- ActiveCell in VBA in Excel
- Special Cells Method in VBA in Excel
- UsedRange Property in VBA in Excel
- VBA AutoFit (Rows, Column, or the Entire Worksheet)
- VBA Copy Range to Another Sheet + Workbook
- VBA Enter Value in a Cell (Set, Get and Change)
- VBA Insert Column (Single and Multiple)
- VBA Named Range | (Static + from Selection + Dynamic)
- VBA Range Offset
- VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
- VBA Wrap Text (Cell, Range, and Entire Worksheet)
- VBA Check IF a Cell is Empty + Multiple Cells
⇠ Back to What is VBA in Excel
Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
Range.ClearContents method (Excel) |
vbaxl10.chm144095 |
vbaxl10.chm144095 |
excel |
Excel.Range.ClearContents |
8c957fdd-e99d-ca0e-7d2c-4cb1db62639a |
05/10/2019 |
high |
Range.ClearContents method (Excel)
Clears formulas and values from the range.
Syntax
expression.ClearContents
expression A variable that represents a Range object.
Return value
Variant
Example
This example clears formulas and values from cells A1:G37 on Sheet1, but leaves the cell formatting and conditional formatting intact.
Worksheets("Sheet1").Range("A1:G37").ClearContents
[!includeSupport and feedback]
Return to VBA Code Examples
In this Article
- VBA Clear Cells / Ranges
- VBA ClearContents
- VBA Clear
- VBA Clear Formatting
- Clear Selection
- Clear Entire Sheet
In VBA it’s easy to clear cells or cell properties with the .Clear methods.
VBA Clear Cells / Ranges
Type the following into the VBA Editor.
Range("a1").Clear
This will display all of the Clear methods available to you:
As you can see, You can clear:
- Everything ( .Clear)
- Comments ( .ClearComments)
- Contents ( .ClearContents)
- Formats ( .ClearFormats)
- Hyperlinks ( .ClearHyperlinks)
- Notes ( .ClearNotes)
- Outline ( .ClearOutline)
VBA ClearContents
The most common clear method is ClearContents. ClearContents clears only the contents of cells (cell values / text). It does not clear formatting, comments, or anything else.
Range("b2").ClearContents
ClearContents is the same as pressing the Delete key on your keyboard.
You can also clear the contents of an entire range of cells:
Range("b2:c10").ClearContents
VBA Clear
Clear will clear all cell properties from a cell:
Range("b2").Clear
VBA Clear Formatting
To clear cell formatting use ClearFormats
Range("b2").ClearFormats
Clear Selection
To clear the current selection:
Selection.Clear
Clear Entire Sheet
To clear an entire worksheet:
Sheets("Sheet1").Cells.Clear
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
ClearContents is a method in VBA used to delete or remove the values stored in the cells provided to it. This method makes the cell range empty. It is used with the range property to access the specified cell range. An example of this method is range(“A1:B2”). The ClearContents method will clear the contents of cells from A1 to B2.
In Excel, adding and deleting data is a common routine task.
Sometimes we delete a single cell value, sometimes many cell values, and sometimes we may require to delete the entire worksheet content. This article will show you how to use the “ClearContents” method in Excel VBA. In VBA, we have many methods to do this, like “Clear,” “Delete,” and “Clear Contents.”
Table of contents
- Excel VBA Clear Contents
- What are Clear Contents in Excel VBA?
- Difference Between Clear & Delete Methods
- Use VBA Clear Contents Method to Retain Formatting of Cells
- Loop Through all the Worksheets and Clear Contents of Specific Range
- Recommended Articles
What are Clear Contents in Excel VBA?
Before we tell you about ClearContents in VBA, let me show how we can delete or clear off the data in the specific range.
Look at the below data.
Now, if we want to clear off cells A1 to C3, we need first to mention the range of cells using the VBA RANGERange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more object.
Code:
Range (“A1:C3”)
After mentioning the range of cells by using the RANGE object, we need to select the method “Clear” to clear off the mention of the cell values.
Code:
Range (“A1:C3”).Clear
It will clear off the mentioned cell values.
Code:
Sub Clear_Example() Range("A1:C3").Clear End Sub
Apart from the clear method, we can also use the “DELETE” method.
Code:
Range (“A1:C3”).Delete
It will delete the mentioned cell values, just like our clear method.
You can use the VBA CELLS propertyCells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel cells.read more with a worksheet name if you want to delete all the cell’s data.
Worksheets(“Sheet1”).Cells.Delete
Worksheets(“Sheet1”).Cells.Clear
Both the above codes will delete the entire worksheet “Sheet1” data. In addition, it will delete the cell values from the first cell to the last cell of the worksheet.
If you want to delete the present sheet cells, you can use the Active Sheet object.
ActiveSheet.Cells.Delete or ActiveSheet.Cells.Clear
Difference Between Clear & Delete Methods
We know this question should have already played in your mind.
However, there is a difference between these two methods.
When you use the method “Delete,” it will delete the cell, and the below cell will take over the position of the deleted cell.
For example, look at the below image.
Now, we will use the delete method to delete cell A1.
Code:
Sub Clear_Example() Range("A1").Delete End Sub
We will run this code and see what happens.
Look what happened here. As we said, when we deleted cell A1, it got deleted, but cell A2 moved one cell up and occupies the deleted cell. So, it will lead to a data mismatch. So, be careful while using the “Delete” method.
Now, for the same data, we will clear the method.
Code:
Sub Clear_Example() Range("A1").Clear End Sub
Now, see what happens when we run this code.
This code has just vacated cell A1 without altering other cells. Therefore, this looks like a proper method to delete only the part of the cells of the entire data range.
Use VBA Clear Contents Method to Retain Formatting of Cells
If you have observed the previous two methods, those two methods not only deleted or cleared off the cells provided. It also deleted the formatting of the cells we have provided.
To retain the formatting of the cells, we need not use neither “Delete” nor “Clear,” but we need to use the VBA “ClearContents” method.
When you enter the range of cells using a RANGE object, it will show all its properties and methods.
We can access “Delete,” we can access “Clear,” and we can also access “ClearContents” methods.
Select this method.
Code:
Sub Clear_Example() Range("A1:C3").ClearContents End Sub
It will clear content from A1 to C3 cell, but we will have all the existing formatting.
As you can see in the above picture, we have cell color in VBA, borders, and every formatting associated with those mentioned cells.
Similarly, we can clear the contents of other sheets as well.
Worksheets(“Sheet1”).Range(“A1:D10”).ClearContents
It will clear the contents from cells A1 to D10 in sheet “Sheet1”.
Similarly, we can delete the other open workbook cells as well.
Workbooks(“Book1.xlsx”).Worksheets(“Sheet1”).Range(“A1:D10”).ClearContents
Loop Through all the Worksheets and Clear Contents of Specific Range
Assume you have many sheets in your workbook. You want to delete the range of cells from A1 to C15. To do this, we must use For Each Loop in VBAVBA For Each Loop helps the user to inspect and analyze the groups of objects or values individually. It even facilitates performing the specific activity for every object or value by passing a statement or group of statements in this reference.read more in all the sheets.
The below code will do the job.
Code:
Sub Clear_All() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Range("A1:C15").ClearContents Next Ws End Sub
Note: You can change the range of cells as per your wish.
If you want to clear off the entire worksheet data, then you need to use the code below.
Code:
Sub Clear_All() Dim Ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets Ws.Cells.ClearContents Next Ws End Sub
You can download this VBA Clear Contents Excel template here – VBA Clear Contents Template.
Recommended Articles
This article has been a guide to VBA Clear Contents. Here, we learn how to use the Clear, Delete, and ClearContents method in VBA to clear data in Excel and some simple to advanced examples. Below are some useful Excel articles related to VBA: –
- New Line in VBA MsgBox
- How to Use Timer in VBA?
- Excel VBA Break For Loop
- Excel VBA Do Loop
How to clear contents of Excel cells/range using VBA
The ClearContents is an Excel function that can be used in VBA to remove values and formulas in the range.
However, formatting and conditional formatting remain intact when using the ClearContents function.
An example of removing the value of A2 cell
Following sheet is used for our examples in this tutorial:
We will remove the A2 cell value by using the ClearContents function as follows:
Sub clear_content_ex() Range(«B3»).ClearContents End Sub |
Result:
Removing multiple cells’ data
We will delete data from A3 to B6 range of cells:
Sub clear_content_ex() ‘Clear values of range of cells Range(«A3:B6»).ClearContents End Sub |
Result:
Note: If you try to recover deleted value by Ctrl+Z, or Undo from the menu – this will not work.
Clear contents from some other sheet than active
If you want to remove values from another sheet than the active one (or just want to specify the sheet name), you can do this as follows:
Sub clear_content_ex() ‘Clear values of another sheet than active Worksheets(«Sheet2»).Range(«A3:B4»).ClearContents End Sub |
How did it work?
- Suppose, our active sheet is “Sheet1”.
- We specified “Sheet2” in the Worksheets object.
- This code will clear contents of the Sheet2’s A3 to B4 cells.
Check if the formula is also removed
In this sheet, we used TRIM() formula in C2 to C6 cells:
You can see, we also applied formatting to the C column cells along with the Trim formula.
After executing the ClearContents function, let us see what result we get:
Sub clear_content_ex() ‘Clear values and formulas Worksheets(«Sheet3»).Range(«C2:C6»).ClearContents End Sub |
Result:
You can see:
- Values are gone.
- Formulas are gone.
- The formatting is still remains.