Adding table in sheet to Power Query VBA

de3pster

New Member
Joined
Dec 17, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like to add my current table in the active worksheet to power query and create an open connection. Right now I have taken reference to a code from online but this code looks at all the worksheets in the workbook and adds all to the power query but I am just looking to only add the active sheet if thats possible. Also wondering if I can do a step further and add it to my appended query as well. I say active sheet because I am importing a new sheet every week so I want it to be automated. Any help would be appreciated. Thank you.

This is what I have now

VBA Code:
Dim wss As Worksheet
Set wss = ActiveSheet
Dim lo As ListObject
Dim sName As String
Dim sFormula As String
Dim wq As WorkbookQuery
Dim bExists As Boolean

    Set wb = ActiveWorkbook

         For Each ws In ActiveWorkbook.Worksheets
         For Each lo In ws.ListObjects
        
         sName = lo.Name
         sFormula = "Excel.CurrentWorkbook(){[Name=""" & sName & """]}[Content]"
       

          'Add query
          wb.Queries.Add Name:=sName, _
                         Formula:="let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""" & sName & """]}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
          'Add connection
          wb.Connections.Add2 Name:="Query - " & sName, _
                              Description:="Connection to the '" & sName & "' query in the workbook.", _
                              ConnectionString:="OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & sName & ";Extended Properties=""""", _
                              CommandText:="SELECT * FROM [" & sName & "]", _
                              lCmdtype:=2, _
                              CreateModelConnection:=False, _
                              ImportRelationships:=False
                              
Next lo
Next ws
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
When you say "current table" do you mean specifically the currently selected table, or is it the only table on the current sheet? Essentially, you'll need to remove the two For Each .. Next loops and just use something like:

Set lo = Activesheet.Listobjects(1)
 
Upvote 0
Solution
When you say "current table" do you mean specifically the currently selected table, or is it the only table on the current sheet? Essentially, you'll need to remove the two For Each .. Next loops and just use something like:

Set lo = Activesheet.Listobjects(1)
Hi, thank you so much for your response. Yes it is the only table in the sheet.

Your code works!! Thanks so much. Can I just ask is it possible to now insert that query into an appended query. For example I have an appended query named Staging. Is it possible to add this directly into that in VBA?
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top