VBA & Macro Problems

Clear Cells Based on an Earlier Date With Excel VBA

Phi Hung VoPhi Hung Vo Nov 18, 2026 868 views

Question details

Clear the contents of columns C through F when the date in column F is earlier than today.

Clear Cells Based on an Earlier Date With Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Run a VBA macro from a button or automatically when the worksheet is activated.
Observed behavior
Rows with a valid date before today in column F should have their contents cleared in columns C through F.
Before you start

Open the workbook in desktop Excel, save it as an Excel Macro-Enabled Workbook (.xlsm), and enable macros if prompted. Check the worksheet name and the row range you want the macro to inspect; the example uses Sheet1 and rows 2 through 1000.

Solution 1Recommended

Create a VBA Macro and Run It From a Button

Use a button to clear matching rows whenever you choose.

The macro checks for valid dates before today and clears cell contents, including formulas, without removing formatting. Dates equal to today are not cleared.

1
Open the VBA editor

In desktop Excel, open the Developer tab and select Visual Basic. If Developer is hidden, enable it through Excel's ribbon customization settings.

2
Insert the macro

In the VBA editor, select your workbook and choose Insert > Module. Paste this code: Sub Clear_C_To_F() Dim ws As Worksheet Dim i As Long Dim v As Variant Set ws = ThisWorkbook.Worksheets("Sheet1") For i = 2 To 1000 v = ws.Cells(i, 6).Value If Not IsError(v) Then If IsDate(v) Then If CDate(v) < Date Then ws.Range(ws.Cells(i, 3), ws.Cells(i, 6)).ClearContents End If End If End If Next i End Sub

3
Adjust the worksheet and rows

In the module, replace "Sheet1" with the exact name of the worksheet to modify. Change the limits in For i = 2 To 1000 if your data uses a different row range.

4
Add and assign a button

Return to the worksheet, select Developer > Insert > Button under Form Controls, and draw the button. In the Assign Macro dialog, select Clear_C_To_F and click OK.

5
Run the macro

Click the button on the worksheet. The macro clears columns C:F for rows whose column F contains a valid date before today.

Create a VBA Macro and Run It From a Button
Check the result range: The macro clears contents in all four cells of each matching row, not just the date in column F. Blank, invalid, and error values in column F are skipped.
Free Microsoft Office alternative

Work With Date-Based Spreadsheets in WPS Office

WPS Office is a free, lightweight, easy-to-use alternative for working with spreadsheets in a familiar interface. Open compatible Excel files to make moving everyday spreadsheet work straightforward. VBA support can vary by edition and platform, so verify macro compatibility before relying on this solution.

Review and edit date-based spreadsheet dataOpen and edit Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Will the macro clear a date that is today?

No. The condition CDate(v) < Date matches only dates and times earlier than the start of today.

Does ClearContents remove cell formatting?

No. Range.ClearContents removes values and formulas but leaves cell formatting in place.

How do I check more than 1,000 rows?

In the VBA module, change the upper limit in For i = 2 To 1000 to the last row you want to inspect.

Why does the automatic macro not run when I change a date?

Worksheet_Activate runs when you activate the sheet, not whenever a cell changes. Switch to another sheet and back to trigger the event, with macros enabled.

What happens if column F is blank or contains an invalid date?

The example checks IsDate before comparing values, so blank cells, invalid dates, and error values are skipped.