Copy Power Query Results into an Excel Table with VBA
Question details
Populate four Excel table columns from a Power Query result while retaining a manually entered fifth column and filtering the query with a worksheet value.

- Product
- Microsoft Excel Power Query and VBA
- Device & OS
- not provided
- Scenario
- A worksheet selection controls a query filter, and the returned rows must be copied into a separate Excel table that includes manual entries.
- Observed behavior
- The destination table should follow the query's changing row count while keeping each manual entry associated with the correct record.
Identify the source query output table, destination table, filter cell, and a unique text key in the first returned column. The example below assumes four source columns, five destination columns, no destination totals row, and Excel for Windows for the VBA dictionary.
Refresh the query and preserve manual entries by row key
Use this approach when query rows may change order or count and manual values must stay with matching records.
The macro below preserves fifth-column values for keys present in both the old destination and the refreshed result. A value cannot be retained by position alone when rows are added, removed, or reordered.
This example stops if the query returns no rows. Entries for keys excluded by the current filter are not retained in the destination table; store those entries in a separate persistent table if they must be recovered when the filter changes.
Enter the filter value in a worksheet cell, select the cell, and use the Name Box next to the formula bar to name it SelectedFilter. In Data > Queries & Connections, open the query in Power Query Editor. Add a filter step that reads the named cell; for example, replace the previous step and column names in this formula: = Table.SelectRows(#"Previous Step", each [Category] = Excel.CurrentWorkbook(){[Name="SelectedFilter"]}[Content]{0}[Column1]). Make sure the cell value and query column have compatible data types, then select Close & Load.
Select each table and use Table Design > Table Name to identify its name. For the example, use QueryOutput for the four-column query output and DestinationTable for the separate five-column table. Ensure the first column contains unique, nonblank text keys in both tables, turn off the destination totals row, and leave room below the destination for additional rows.
Press Alt+F11, choose Insert > Module, and paste the following code. Change the sheet and table names to match your workbook. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Sub CopyQueryResults() Dim src As ListObject, dst As ListObject Dim saved As Object, seen As Object Dim data As Variant, oldData As Variant Dim key As String, n As Long, oldCount As Long, i As Long On Error GoTo Failed Set src = ThisWorkbook.Worksheets("QuerySheet").ListObjects("QueryOutput") Set dst = ThisWorkbook.Worksheets("DestinationSheet").ListObjects("DestinationTable") If src.ListColumns.Count <> 4 Or dst.ListColumns.Count <> 5 Then Err.Raise 5, , "Check the table column counts." If dst.ShowTotals Then Err.Raise 5, , "Turn off the destination totals row." If Not src.QueryTable.Refresh(BackgroundQuery:=False) Then Err.Raise 5, , "Query refresh was canceled or failed." If src.DataBodyRange Is Nothing Then Err.Raise 5, , "The query returned no rows; the destination was not changed." data = src.DataBodyRange.Value2 n = UBound(data, 1) Set saved = CreateObject("Scripting.Dictionary") Set seen = CreateObject("Scripting.Dictionary") oldCount = dst.ListRows.Count If oldCount > 0 Then oldData = dst.DataBodyRange.Value2 For i = 1 To oldCount If IsError(oldData(i, 1)) Then Err.Raise 5, , "Invalid destination key." key = CStr(oldData(i, 1)) If Len(Trim$(key)) = 0 Or saved.Exists(key) Then Err.Raise 5, , "Destination keys must be unique and nonblank." saved.Add key, oldData(i, 5) Next i End If For i = 1 To n If IsError(data(i, 1)) Then Err.Raise 5, , "Invalid query key." key = CStr(data(i, 1)) If Len(Trim$(key)) = 0 Or seen.Exists(key) Then Err.Raise 5, , "Query keys must be unique and nonblank." seen.Add key, True Next i If n > oldCount Then If Application.CountA(dst.HeaderRowRange.Offset(oldCount + 1, 0).Resize(n - oldCount, 5)) > 0 Then Err.Raise 5, , "Clear the cells below the destination table first." ElseIf n < oldCount Then dst.DataBodyRange.Rows(n + 1).Resize(oldCount - n, 5).ClearContents End If dst.Resize dst.HeaderRowRange.Resize(n + 1, 5) For i = 1 To n dst.DataBodyRange.Cells(i, 1).Resize(1, 4).Value2 = src.DataBodyRange.Cells(i, 1).Resize(1, 4).Value2 key = CStr(data(i, 1)) If saved.Exists(key) Then dst.DataBodyRange.Cells(i, 5).Value2 = saved(key) Else dst.DataBodyRange.Cells(i, 5).ClearContents End If Next i Exit Sub Failed: MsgBox Err.Description, vbExclamation, "Copy query results" End Sub
Change the SelectedFilter cell, then run CopyQueryResults from Developer > Macros. Check that the four query columns match the refreshed output and that fifth-column entries remain beside their matching keys.

Work with Excel tables in WPS Spreadsheet
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook editing to WPS. The Excel Power Query and VBA workflow described above may not work the same way in WPS, so test automation before migrating.

Frequently Asked Questions
Why should I match manual entries by a key instead of row number?
A refresh can add, remove, or reorder query rows. Matching on a unique key keeps the fifth-column value with the same record rather than leaving it at an old row position.
How do I change the query filter without editing Power Query?
Change the worksheet cell named SelectedFilter, then run the macro. The query's filter step reads that cell when the query refreshes.
What happens to manual entries when a record is filtered out?
The example retains entries only for keys present in the refreshed result. To recover entries for filtered-out records later, keep them in a separate persistent table keyed by record ID.
Why does the macro stop when the query returns no rows?
The example stops to avoid clearing the destination when a filter unexpectedly returns nothing. Adjust that behavior only after deciding how an empty result should affect manual entries.




