How to Automatically Add Dates to Imported Rows using Excel Macro
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.

- 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.
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.
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).
Press 'Alt + F11' in your spreadsheet to open the Visual Basic for Applications (VBA) editor.
Find the module containing the macro that imports your data into the dashboard.
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`.
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.
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.

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. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and enable the Developer tab to access Macro features.
- 2. Open the VBA Editor: Click 'Visual Basic' in the Developer tab or press Alt+F11 to open the code editor.
- 3. Paste and Run Your Code: Insert your data import macro along with the dynamic date logic, then click 'Run' to automate your dashboard.

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.




