Record Refreshing Web Data History in Excel with VBA
Question details
Save calculated results and timestamps from refreshing web data without losing earlier values.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A web data source refreshes periodically, and the user wants to keep a timestamped history of a calculated total.
- Observed behavior
- The calculated value changes as the source refreshes, so the user needs each result stored in a new history row.
In desktop Excel, identify the worksheet table containing the web query and the cell containing the calculated result. The event-based method below requires a query loaded to a worksheet table with a QueryTable object, an open workbook, and enabled VBA macros.
Append results automatically after a web query refresh
Use a VBA refresh event to record the calculated value and timestamp after each successful refresh of a supported worksheet query.
The example assumes the query table is named WebData on a sheet named Data, the calculated result is in Data!B2, and a sheet named History stores the log. Change these names and the cell reference to match your workbook.
This method logs refreshes of the specified QueryTable only. It does not monitor connection-only queries or guarantee that other independent queries have finished refreshing.
Create a worksheet named History. Enter Timestamp in A1 and Result in B1. On the Data sheet, select the imported table and check its name on the Table Design tab.
Press Alt+F11 to open the VBA editor. Choose Insert > Class Module, set its name to CQueryLogger in the Properties window, and paste: Public WithEvents Query As Excel.QueryTable Private Sub Query_AfterRefresh(ByVal Success As Boolean) Dim result As Variant Dim nextRow As Long If Not Success Then Exit Sub Application.CalculateFull result = ThisWorkbook.Worksheets("Data").Range("B2").Value If IsError(result) Then Exit Sub If IsEmpty(result) Or Not IsNumeric(result) Then Exit Sub With ThisWorkbook.Worksheets("History") nextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 .Cells(nextRow, "A").Value = Now .Cells(nextRow, "A").NumberFormat = "yyyy-mm-dd hh:mm:ss" .Cells(nextRow, "B").Value = result End With End Sub
In the VBA editor, choose Insert > Module and paste the following code. Replace Data and WebData if your sheet or table has different names: Public Logger As CQueryLogger Public Sub StartQueryLogger() Set Logger = New CQueryLogger Set Logger.Query = ThisWorkbook.Worksheets("Data").ListObjects("WebData").QueryTable End Sub
In the VBA editor's Project pane, double-click ThisWorkbook and paste: Private Sub Workbook_Open() StartQueryLogger End Sub Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros when prompted, then run StartQueryLogger from Developer > Macros to initialize it in the current session.
Use Data > Refresh All, then check History for a new timestamp and result. To refresh periodically, open Data > Queries & Connections, access the query's connection properties, and set a refresh interval if that option is available.

Record a snapshot manually after each refresh
Use this method when VBA is unavailable or you only need occasional history entries.
Keep spreadsheet snapshots in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and move existing worksheets into WPS Spreadsheets; the Excel-specific VBA refresh listener above is not a fix that WPS can apply to Microsoft Excel.

Frequently Asked Questions
Will the VBA code overwrite earlier results?
No. After a successful refresh, the code finds the last used cell in column A of the History sheet and writes the timestamp and result to the next row.
Why did a refresh not add a history row?
Confirm that macros are enabled and run StartQueryLogger from Developer > Macros. Also check that the table and sheet names match the code, the refresh succeeded, and the result cell contains a number.
Can I use this event with a connection-only Power Query?
Not as written. The example listens to a QueryTable associated with a worksheet table. A connection-only query needs a different monitoring approach or a manual snapshot after refresh.
Will the timestamp change when Excel recalculates?
No. VBA writes the current time as a fixed value using Now. By contrast, a NOW() formula entered directly in a worksheet can change when the workbook recalculates.
Does the history update while the workbook is closed?
No. The workbook must be open, macros must be enabled, and the listener must be initialized for the event to record a refresh.




