VBA & Macro Problems

Use Excel VBA to Autofill Dates Based on Column B

Adam DavisAdam Davis Nov 14, 2026 869 views

Question details

Fill column A with dates for rows containing data in column B, even when the number of rows changes.

Use Excel VBA to Autofill Dates Based on Column B
Product
Microsoft Excel
Device & OS
not provided
Scenario
A spreadsheet changes in length, and the user wants either a date formula that updates daily or a fixed date value for populated rows.
Observed behavior
Column A should receive dates for rows where column B contains data, up to the last populated row in column B.
Before you start

These examples assume row 1 contains headers and data begins in row 2; change the starting row in the code if needed. Check whether column A already contains values, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to retain the macro.

Solution 1Recommended

Fill column A with a dynamic TODAY formula

Use this method when the displayed date should update each day.

The macro finds the last populated row in column B and checks each row individually, so gaps in column B are skipped. It overwrites existing values in column A for matching rows.

1
Open the VBA editor

In Excel, open the target worksheet, select Developer > Visual Basic, and choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub FillTodayFormulas() Dim ws As Worksheet Dim lastRow As Long, r As Long Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row For r = 2 To lastRow If Len(ws.Cells(r, "B").Value) > 0 Then ws.Cells(r, "A").Formula = "=TODAY()" End If Next r End Sub

3
Run and format the dates

Return to the target worksheet, choose Developer > Macros, select FillTodayFormulas, and click Run. If necessary, select column A and choose Home > Number Format > Short Date.

Fill column A with a dynamic TODAY formula
Run the macro for new rows: TODAY() updates its displayed date when Excel recalculates, but the macro must be run again to add formulas to newly populated rows. Cells in column A are not cleared when column B is blank.
Free Microsoft Office alternative

Work with date formulas in WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use date formulas while moving between Office applications; VBA macro support may vary by WPS edition and configuration.

Use TODAY() formulas to display a date that updates with recalculation.Open and edit compatible Microsoft Excel .xlsx files.Work in a familiar spreadsheet interface for an easier transition from Excel.Use a free, lightweight office suite for everyday documents and spreadsheets.
microsoft office alternative - wps office

Frequently Asked Questions

Why do the dates change each day?

The formula =TODAY() returns the current date when Excel recalculates. Use the fixed-date macro with the VBA Date value if the date should stay unchanged.

Will the macro fill dates automatically when I add rows?

Not by itself. After adding data to column B, activate the worksheet and run the macro again from Developer > Macros.

What happens if column B has blank rows?

Both macros check each row through the last populated row and skip blank cells in column B. They do not clear any existing values in column A for those rows.

How do I keep the macro in my workbook?

Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm). Enable macros when prompted only if you trust the workbook.

What does xlUp do in the last-row code?

The expression ws.Cells(ws.Rows.Count, "B").End(xlUp).Row finds the last populated cell by moving upward from the bottom of column B. The constant uses a lowercase L after the x, not the number 1.