Data Import & Export

Save Excel Web Query Cell Values Before Each Refresh

Bushra ParveenBushra Parveen Nov 25, 2026 868 views

Question details

The user wants to record current cell values before an hourly Excel web query refreshes them.

Save Excel Web Query Cell Values Before Each Refresh
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.
Before you start

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.

Solution 1Recommended

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.

1
Prepare the history sheet

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.

2
Create the event class

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

3
Connect the query to the handler

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

4
Enable and test logging

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.

5
Set the hourly refresh

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.

Log a cell value with an Excel query refresh event
Understand what is counted: The code logs before each refresh attempt, even if the refresh later fails. An hourly schedule produces 24 rows in a day only if 24 refresh events occur; manual refreshes add rows too. To record more cells, add a history column and a corresponding value assignment for each cell.
Free Microsoft Office alternative

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.

Review and analyze recorded values in WPS SpreadsheetOpen and save Microsoft Excel .xlsx filesWork in a familiar spreadsheet interfaceMove existing spreadsheet data into a lightweight Office suite
microsoft office alternative - wps office

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.