Save Excel Web Query Cell Values Before Each Refresh
Question details
The user wants to record current cell values before an hourly Excel web query refreshes them.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- An hourly web query updates selected cells, and the user needs a historical table for statistical analysis.
- Observed behavior
- Each refresh replaces the current values; the desired result is a history containing one recorded value per hourly refresh.
Identify the worksheet, query table, and cell to record, and add a worksheet named History. The VBA example requires desktop Excel, a macro-enabled workbook, and a query exposed as a worksheet QueryTable; it may not apply to every Power Query connection.
Log a cell value with an Excel query refresh event
Use a VBA event handler to append a timestamp and the current cell value immediately before a supported query table refreshes.
This example watches the first QueryTable on Sheet1 and records cell B2. Replace Sheet1 and B2 with your worksheet and cell names. If the query is not available through QueryTables, its connection type needs a different approach.
On the History sheet, enter Timestamp in A1 and Value in B1. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) using File > Save As.
Open Developer > Visual Basic, choose Insert > Class Module, and name the class CQueryLogger in the Properties window. Paste this code into the class: Public WithEvents WatchedQuery As Excel.QueryTable Private Sub WatchedQuery_BeforeRefresh(Cancel As Boolean) Dim history As Worksheet Dim nextRow As Long Set history = ThisWorkbook.Worksheets("History") nextRow = history.Cells(history.Rows.Count, "A").End(xlUp).Row + 1 history.Cells(nextRow, "A").Value = Now history.Cells(nextRow, "B").Value = ThisWorkbook.Worksheets("Sheet1").Range("B2").Value End Sub
In the Visual Basic editor, open ThisWorkbook and paste the following code: Public QueryWatcher As CQueryLogger Private Sub Workbook_Open() StartLogging End Sub Then choose Insert > Module and paste: Public Sub StartLogging() Set ThisWorkbook.QueryWatcher = New CQueryLogger Set ThisWorkbook.QueryWatcher.WatchedQuery = ThisWorkbook.Worksheets("Sheet1").QueryTables(1) End Sub
Save the workbook and enable macros if prompted. To initialize the handler in the current session, run StartLogging from Developer > Macros. Then use Data > Refresh All and check that a new timestamp and the pre-refresh B2 value appear on History.
In Data > Queries & Connections, open the relevant connection's Properties and, if available, select Refresh every and enter 60 minutes. Keep the workbook open with macros enabled for the event handler to run.

Ask the Excel community about unsupported query types
Use this option if the query is not exposed as a QueryTable or you need help adapting the event code.
Analyze your query history with WPS Spreadsheet
WPS Office is a free, lightweight, easy-to-use Office alternative with a familiar interface and Excel format compatibility for moving existing spreadsheets into WPS. It does not fix Excel's query refresh event or guarantee compatibility with this VBA automation.

Frequently Asked Questions
Will the macro run when the workbook is closed?
No. The workbook must be open in desktop Excel with macros enabled and the event handler initialized for it to log a refresh.
How do I record more than one cell?
Add a header for each cell on the History sheet. In CQueryLogger, add a history.Cells(nextRow, column).Value assignment for each source cell in the BeforeRefresh procedure.
Why does the example fail at QueryTables(1)?
The named worksheet may not contain a QueryTable, or the connection may use a different query type. Check the worksheet and connection in Data > Queries & Connections; a different approach may be needed for Power Query.
Can I use this VBA code in Excel for the web?
No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in desktop Excel and enable macros to use the event handler.
Does every history row represent a successful refresh?
No. The BeforeRefresh event records the value before the refresh attempt, so a row can be added even when the subsequent refresh fails.




