To clear only cell values while preserving formulas in Excel using VBA, you use the Value property of a range and set it to Empty (or vbNullString), leaving the Formula property untouched. This is useful for resetting input cells before re-running calculations or cleaning up data entry fields without breaking existing logic.

What You'll Need

  • Excel installed (Windows or Mac; steps differ slightly for Mac)
  • A workbook with cells containing both values and formulas
  • Developer tab enabled in Excel (required to access the VBA editor)
  • Basic understanding of Excel cell references and ranges

Step-by-Step

  1. Open your Excel workbook and ensure it is saved in a macro-enabled format (.xlsm) if you plan to keep the macro. If you only need to run this once, saving as .xlsx is fine, but macros won’t be preserved.
  2. Press Alt + F11 (Windows) or Fn + Option + F11 (Mac) to open the Visual Basic for Applications (VBA) editor.
  3. In the VBA editor, go to Insert > Module to create a new standard module. This is where your code will live.
  4. Copy and paste the following code into the module window:
    Sub ClearValuesOnly()
        Dim rng As Range
        Dim cell As Range
        
        ' Set the range you want to clear values from—change this to your target range
        Set rng = ActiveSheet.Range("A1:D100") ' Example: adjusts as needed
        
        For Each cell In rng
            If Not IsEmpty(cell) Then
                ' Check if the cell contains a formula (not just a static value)
                If Left(cell.Formula, 1) <> "=" Then
                    cell.Value = Empty
                End If
            End If
        Next cell
    End Sub
  5. Customize the range in the code. Replace ActiveSheet.Range("A1:D100") with your desired range (e.g., Range("InputRange") if you’ve defined a named range, or Worksheets("Sheet2").Range("B2:E50") for a specific sheet and area).
  6. Press F5 while in the VBA editor to run the macro, or return to Excel and run it via Alt + F8, selecting ClearValuesOnly, then clicking Run.
  7. Verify the results: cells with static values (e.g., typed numbers, text) should now be blank, but cells containing formulas (e.g., =SUM(A1:A10)) remain intact and still display results.

Common Mistakes

  • Using ClearContents instead of setting .Value = Empty: Range("A1").ClearContents removes *both* values and formulas. This is the most frequent error—ensure your code targets only the Value property.
  • Not checking for empty cells first: Skipping the IsEmpty(cell) check can cause unnecessary processing and potential errors on formatted but blank cells.
  • Overlooking cells with formulas that return empty strings: A cell with ="" will be skipped because its Formula starts with =, but if your goal is to clear *all* non-formula content—including cells that *look* blank due to formulas returning ""—you must adjust your logic accordingly.
  • Accidentally running on protected sheets: If the worksheet is protected, the macro will fail unless you unprotect it first. Use ActiveSheet.Unprotect (with a password if needed) before the loop, then re-protect afterward.

Tips

  • Leverage SpecialCells for speed: For large ranges, use SpecialCells(xlCellTypeConstants) to target *only* constant (non-formula) cells directly:
    Sub ClearValuesOnly_Faster()
        On Error Resume Next
        ActiveSheet.Range("A1:D100").SpecialCells(xlCellTypeConstants).Value = Empty
        On Error GoTo 0
    End Sub
    This avoids looping cell-by-cell and runs much faster. It skips formula cells entirely, so no Left(cell.Formula, 1) check is needed.
  • Handle errors gracefully: Use On Error Resume Next when working with SpecialCells because if no constants exist in the range, it throws an error. Reset error handling with On Error GoTo 0 immediately after.
  • Test on a copy first: Always duplicate your workbook before running macros—especially when clearing data—to avoid accidental loss.
  • Use named ranges for clarity: Define ranges like InputCells in Excel (Formulas > Define Name), then reference them in VBA as Range("InputCells"). This makes your code readable and easier to maintain.

Troubleshooting

  • Macro doesn’t run, or Excel shows a security warning: Ensure macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros (temporarily, for testing only). For production, use Disable all macros with notification and enable per workbook.
  • Values still remain after running: Double-check your range reference. Did you use ActiveRange vs. ActiveSheet.Range? Did the sheet name change? Also verify no cells contain formulas that evaluate to constants (e.g., =1+1 is a formula and will be preserved—this is correct behavior).
  • Unexpected blank cells with formulas: If a formula cell becomes blank, it may be because the formula itself returned "" (empty string), which displays as blank but *is* a formula. Your code correctly preserved it. To clear *those too*, modify the check to If Left(cell.Formula, 1) <> "=" Or cell.Formula = "\"\"" Then—though be cautious, as this changes the definition of "not a formula."">
  • Run-time error '1004': Application-defined or object-defined error: This usually means the range is invalid (e.g., A1:ZZ9999 exceeds Excel’s limits) or the sheet is protected. Confirm the range syntax and unprotect the sheet first if needed.

Frequently Asked Questions

Can I clear values across all sheets at once?

Yes. Loop through all worksheets and apply the same logic. Example:

Sub ClearValuesAcrossSheets()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        On Error Resume Next
        ws.Range("A1:D100").SpecialCells(xlCellTypeConstants).Value = Empty
        On Error GoTo 0
    Next ws
End Sub

Does this work on Mac?

Yes, but the VBA editor shortcut is Fn + Option + F11, and macro security paths differ slightly (File > Office > Security > Macro Security). The code itself is identical on Windows and Mac Excel.

What if some cells use array formulas?

Array formulas (e.g., =SUM(A1:A10*B1:B10) entered with Ctrl+Shift+Enter) are still formulas. Your code will preserve them correctly because Left(cell.Formula, 1) returns =. No extra handling is needed.

How do I clear only numeric values, leaving text?

Modify the loop to check the data type:

For Each cell In rng
    If Not IsEmpty(cell) Then
        If VarType(cell.Value) = vbDouble Or VarType(cell.Value) = vbInteger Then
            cell.Value = Empty
        End If
    End If
Next cell
Use VarType to distinguish numbers (vbDouble) from text (vbString).

Can I undo this action?

Yes—if you run the macro *before* saving the workbook. Excel’s undo stack remains active until you save. After saving, you must restore manually or from a backup. Always save a copy before running value-clearing macros.

Will this affect conditional formatting or data validation?

No. Clearing the Value property only removes cell contents. Formatting, data validation rules, and comments remain untouched unless you explicitly clear them with methods like .ClearFormats or .ClearComments.