Excel VBA Clear Cell Contents: Methods and Examples

VBA & macros · Updated December 3, 2026

To clear cell contents with Excel VBA, use Range("A1").ClearContents; this removes the cell’s value or formula while keeping its formatting. To clear a wider area, specify a range such as Range("A1:C10").ClearContents. Save a copy before running a macro, because VBA changes cannot be undone with Ctrl+Z.

What ClearContents removes

The ClearContents method removes formulas and constant values from the specified cells. Constants include numbers, text, dates, logical values, and error values entered in cells. It does not remove formatting, comments or notes, data validation, or conditional formatting. It also does not delete cells or shift neighboring data.

This distinction matters when a worksheet is a form or template. Clearing its input cells can leave borders, number formats, colors, and instructions in place, ready for another entry. A cell containing a formula becomes blank after clearing; a cell with a manually entered number becomes blank in the same way.

To clear both contents and formatting, use Clear instead. To remove formatting alone while retaining values and formulas, use ClearFormats. These methods act on the range you specify, so choose that range carefully.

Basic VBA examples

Place VBA code in a standard module or in the appropriate worksheet or workbook code module, depending on how you intend to run it. For a simple one-time procedure, a standard module is a common choice. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. On a Mac, use Option+F11 (some keyboards require Fn as well), or open the editor from the Developer tab if available.

Sub ClearOneCell()
    ' Remove the value or formula from A1.
    Range("A1").ClearContents
End Sub

To clear several cells, use a contiguous range or a range made from separate areas:

Sub ClearEntryAreas()
    ' Clear two input areas without changing their formatting.
    Range("B2:B10,D2:D10").ClearContents
End Sub

For code that lives in a worksheet module, qualify the range with that worksheet to avoid acting on whichever sheet happens to be active. For example:

Sub ClearInputOnForm()
    Dim ws As Worksheet

    ' Target the worksheet by its tab name.
    Set ws = ThisWorkbook.Worksheets("Form")
    ws.Range("B2:B10").ClearContents
End Sub

Replace “Form” and the cell addresses with the names and locations in your workbook. If a sheet name contains spaces, the quoted worksheet name still works as shown.

Clear only selected or specific cells

To let a user choose cells, use Selection.ClearContents. It acts on the current selection, which can be useful for a general-purpose macro, but it can also clear the wrong cells if the user selects a heading or formula by mistake. A fixed range is safer for a repeatable task.

Sub ClearSelectedCells()
    ' Clear contents only in the current selection.
    Selection.ClearContents
End Sub

You can also target cells that meet a condition. For example, this macro clears constants but leaves formulas untouched in a chosen range. It handles the case where no matching cells exist:

Sub ClearConstantsOnly()
    Dim target As Range
    Dim constants As Range

    ' Set the area to check.
    Set target = ThisWorkbook.Worksheets("Data").Range("A2:D100")

    ' SpecialCells raises an error if no constants are found.
    On Error Resume Next
    Set constants = target.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0

    If Not constants Is Nothing Then constants.ClearContents
End Sub

Here, constants means entered values rather than formulas. To clear formulas instead, use xlCellTypeFormulas in place of xlCellTypeConstants. Test conditional macros on a copy first, especially if the range contains a mix of data types or important formulas.

Choosing the right clearing method

VBA offers several methods with different effects. Choose according to what should remain in the cells:

  • ClearContents: removes values and formulas, while preserving formatting and other cell features.
  • ClearFormats: removes formatting but leaves values and formulas.
  • ClearComments: removes legacy comments; threaded comments are a separate feature and may require different handling depending on Excel’s object model and version.
  • Clear: clears cell contents and formatting, along with other cell information such as comments and hyperlinks. Use it only when a full reset is intended.

“Clear” is not the same as deleting cells. A clear operation leaves the grid structure unchanged. Deleting a range can shift cells up or left and may affect formulas or layout. Likewise, pressing Delete on a selected cell in Excel generally clears its contents without removing the cell itself; VBA’s ClearContents makes that intent explicit.

Common problems and safe use

A frequent mistake is using an unqualified Range reference in a macro that may run while another worksheet is active. In that situation, VBA can target the active sheet rather than the sheet the author intended. Qualify the range with a worksheet variable, as in the example above, and qualify the workbook too when the macro might interact with more than one open file.

Other practical checks include:

  • Confirm the address before running the procedure; a typo or oversized range can erase more data than intended.
  • Check whether the target range includes formulas that must be retained. ClearContents removes them.
  • Account for protected sheets. Excel may prevent the macro from changing locked cells unless protection is removed or the workbook is configured to allow the operation.
  • Use a workbook saved in a macro-enabled format, such as .xlsm, if you need to retain VBA code.
  • Keep a backup. Excel does not provide Ctrl+Z undo for changes made by a VBA macro.

Macros may be blocked by Excel’s security settings or by an organization’s policy. Run code only when you understand what it changes and trust its source. A clear-contents macro does not need special permissions beyond the workbook and sheet protections that apply to the target cells.

Running the macro

After adding a procedure to a standard module, return to Excel and run it from the Macro dialog. On Windows, press Alt+F8, select the macro, and choose Run. On Mac, open the Macro dialog from Tools > Macro > Macros or use the Developer tab if shown; keyboard shortcuts can vary by keyboard configuration. You can also assign a macro to a button, but make sure its label describes the cells it will clear.

For a one-time action, using the worksheet interface may be simpler: select the cells and press Delete. For a repeatable reset, a macro is useful because it can target the same input cells every time, preserve formatting, and be connected to a clearly labeled control.

Frequently Asked Questions

How do I clear cells but keep formatting in VBA?
Use Range("A1:B10").ClearContents, substituting your target range. It removes values and formulas but retains cell formatting.

Does ClearContents remove formulas?
Yes. Formulas are contents, so ClearContents deletes them as well as typed values. If formulas must remain, target only the input cells or identify constants with SpecialCells(xlCellTypeConstants).

What is the difference between Clear and ClearContents?
ClearContents removes values and formulas while leaving formatting in place. Clear performs a broader reset that includes formatting and other cell information.

Can I clear a range on a worksheet that is not active?
Yes. Reference the worksheet explicitly, for example ThisWorkbook.Worksheets("Data").Range("A2:A20").ClearContents. This avoids relying on the active sheet.

Can I undo a VBA clear operation with Ctrl+Z?
No. Excel’s normal undo history does not restore changes made by a macro. Save a separate copy or create a backup before running code that clears data.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.