VBA & Macro Problems

How to Automatically Add Dates to Imported Rows using Excel Macro

Bushra ParveenBushra Parveen Nov 10, 2026 871 views

Question details

The user needs to use a VBA macro to automatically add the current date into a specific column for rows that have just been imported into a dashboard.

Automatically Add Dates to Rows Imported by an Excel Macro
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Importing a variable number of rows into a dashboard worksheet using a macro.
Observed behavior
The macro must determine the last populated row and fill the current date into a specific column for all newly imported records.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled macros in your Trust Center settings before running the VBA code.

Solution 1Recommended

Use VBA to Determine the Last Row and Insert Dates

Identify the boundaries of your imported data using VBA and apply the current date to the target column dynamically.

When you import a variable number of rows, hardcoding cell ranges will not work. Instead, you can use VBA to dynamically find the last row of your dataset.

By determining the first and last row of the newly imported data, you can apply the standard `Date` function to that specific range in your target column (e.g., column BH).

1
Open the VBA Editor

Press 'Alt + F11' in your spreadsheet to open the Visual Basic for Applications (VBA) editor.

2
Locate Your Import Macro

Find the module containing the macro that imports your data into the dashboard.

3
Identify the Last Row

Add the following code snippet immediately after your data import logic to find the last row based on column A: `lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row`.

4
Define the First Row

Ensure you have a variable storing the first row of your newly imported data, for example: `firstRow = 2` or whichever row your new batch of data begins on.

5
Apply the Date to the Column

Add the code to insert the date into column BH for the designated rows: `ws.Range("BH" & firstRow & ":BH" & lastRow).Value = Date`. Ensure `ws` is properly set to your dashboard worksheet.

Use VBA to Determine the Last Row and Insert Dates
Worksheet Object: Always qualify your range objects with the specific worksheet object (e.g., `Dim ws As Worksheet`, `Set ws = ThisWorkbook.Sheets("Dashboard")`) to avoid errors when the macro runs.
Powerful Spreadsheet Editor

Run Macros and Automate Workflows with WPS Spreadsheet

WPS Office Spreadsheet provides excellent support for VBA macros, allowing you to seamlessly automate tasks like inserting dynamic dates into imported rows. Enjoy a familiar interface that handles complex automation with ease.

  1. 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and enable the Developer tab to access Macro features.
  2. 2. Open the VBA Editor: Click 'Visual Basic' in the Developer tab or press Alt+F11 to open the code editor.
  3. 3. Paste and Run Your Code: Insert your data import macro along with the dynamic date logic, then click 'Run' to automate your dashboard.
Full compatibility with Microsoft Excel VBA macros and .xlsm formatsAdvanced developer tools for writing, debugging, and running scriptsLightweight application with high performance for large datasetsFamiliar spreadsheet interface that requires zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

How can I add the date and time instead of just the date?

To add both the date and time, replace the VBA `Date` function with the `Now` function in your code: `ws.Range("BH" & firstRow & ":BH" & lastRow).Value = Now`.

Why does my code return the wrong last row?

This usually happens if column A contains blank cells within the data, or if the imported data does not populate column A. Make sure you reference a column that is guaranteed to have data for every row, such as an ID or Name column.

Can I apply a specific date format to the imported cells via VBA?

Yes. After inserting the date, you can format the cells by adding a line like `ws.Range("BH" & firstRow & ":BH" & lastRow).NumberFormat = "mm/dd/yyyy"` to your macro.

Will this macro overwrite existing dates in my target column?

If your `firstRow` variable is correctly set to the start of the newly imported data, it will only write dates into the new rows. However, if `firstRow` is hardcoded to row 2, it will overwrite all rows from 2 down to the last row.