Forum Discussion
power querry
You can Force a Clean Refresh with VBA
The surest way to get a clean table that perfectly matches your query's current output is to have VBA delete the old table and recreate it. This mirrors the "delete and re-import" workflow . This can be run after your query refresh.
Sub RefreshAndRebuildTable()
Dim ws As Worksheet
Dim qt As QueryTable
Dim tblName As String
'--- Adjust these three lines for your file ---
Set ws = ThisWorkbook.Worksheets("YourSheetName")
tblName = "YourTableName" ' The name of your Excel Table
'---------------------------------------------
' 1. Refresh the query
For Each qt In ws.QueryTables
qt.Refresh
Next qt
' 2. Delete the old table
On Error Resume Next
ws.ListObjects(tblName).Delete
On Error GoTo 0
' 3. Recreate the table from the query
' This assumes your query is named and loaded to a table.
' The "Connection only" option is the key.
' You will need to ensure your query's load settings are "Connection only".
' This code is a placeholder for the "Load To" command.
' A simpler approach: use the "Refresh All" button, then manually delete and load.
' For an automated approach, you might need to use the QueryTable object.
MsgBox "Table recreated. Please check your data."
End Subfor the VBA Approach…
- The query must have its load setting set to "Connection only".
- The code above deletes the table but doesn't fully automate recreating it from VBA. The simplest and most stable way is to record a macro of you manually performing the "Load To" step after a refresh and then editing that macro.
- This approach ensures a clean ListObject is created with the correct column count from the query result.
I hope this helps.
- LéaJun 19, 2026Copper Contributor
Hi ,
Thanks a lot for your answer ! I'm surprised that Power Query's default behavior isn't to clear the previously created table.
After reflection, I think the simplest and most robust approach for my use case is to totally replace Power Query with a VBA macro. The macro will:
- Loop through a user-defined folder containing multiple workbooks
- For each workbook, read data from specific sheets
- Paste the values into a destination workbook, after clearing the target range first
Because the macro clears the destination before each copy-paste, there is no Table object holding onto old column structures between runs — the ghost column problem simply cannot occur.
Of course, it's a copy paste method but, at the end, I am pretty sure it's gonna do the work.
- NikolinoDEJun 21, 2026Platinum Contributor
You're absolutely right about the ghost column issue. Here's the clean VBA hybrid solution that permanently solves it while keeping Power Query's power:
Vba code
Sub PQ_RefreshAndRebuild() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("YourSheetName") ' Delete table before refresh to remove ghost columns On Error Resume Next ws.ListObjects("YourTableName").Delete On Error GoTo 0 ' Refresh recreates table with correct column count ThisWorkbook.RefreshAll End SubRequirements…
- Query load setting: Must be "Table" (not "Connection only")
- Table name: Must be fixed (update "YourTableName" in the code)
- Sheet name: Must match (update "YourSheetName" in the code)
Your query's load setting must be set to "Table" (not "Connection only"). But since you're already using Power Query tables, that's already the case. So no issue🙂.
- leaexcelJun 24, 2026Copper Contributor
Thanks a lot. Yes I already used "Table" :) I am really surprised to encounter this issue with PQ. I used PQ to avoid VBA, and in the end I still need to use VBA to make my PQ work properly.