Clear Cells Based on an Earlier Date With Excel VBA
Question details
Clear the contents of columns C through F when the date in column F is earlier than today.

- 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.
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.
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.
In desktop Excel, open the Developer tab and select Visual Basic. If Developer is hidden, enable it through Excel's ribbon customization settings.
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
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.
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.
Click the button on the worksheet. The macro clears columns C:F for rows whose column F contains a valid date before today.

Run the Macro When the Worksheet Is Activated
Use a worksheet event to run the same macro whenever you switch to the specified sheet.
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.

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.




