Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
zwie
Frequent Visitor

Reuse query connection for new sheet

Hello,

 

I am running power queries from VBA and am having issues reusing my old connections. I am working in a system where we need to delete and recreate the output sheet each time so I can't just refresh the old connections. Even after deleting the sheet, I can see the old connections in the ThisWorkbook.Connections collection, so instead of deleting the connections and recreating them, is it possible to use an old connection to output to a new sheet without creating a new connection?

 

This is the code I use to make the original connections:

Sub loadToWorksheet(query As WorkbookQuery, workingSheet As Excel.Worksheet, outputTableName As String)
  With workingSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & query.Name _
    , Destination:=workingSheet.Range("A1"))
    .Name = outputTableName
    With .QueryTable
      .CommandType = xlCmdDefault
      .CommandText = Array("SELECT * FROM [" & query.Name & "]")
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .PreserveFormatting = False
      .PreserveColumnInfo = True
      .RefreshOnFileOpen = False
      .BackgroundQuery = False
      .RefreshStyle = xlOverwriteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .RefreshPeriod = 0
      .Refresh BackgroundQuery:=False
    End With
  End With
End Sub

 

I have tried adding the old connection to the ListObjects of the new sheet, but the add function can't take in connections.

 

It feels like what I am trying to do is not possible since connections seem to be tied to a sheet, but I wanted to see if I was missing anything.

Thank you!

1 REPLY 1
Anonymous
Not applicable

Hi @zwie ,

 

I am working in a system where we need to delete and recreate the output sheet each time

So are the columns in the VBA always the same?

 

Hope these could help:

VBA Macro to Create Power Query Connections for All Tables - YouTube

Modifying Power Query M code with VBA - Excel UnpluggedExcel Unplugged

 

Best Regards,
Eyelyn Qin

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors