VBA Code for Excel Power Query - change

Radoslaw Poprawski

Active Member
Joined
Jun 19, 2021
Messages
398
Office Version
  1. 365
Platform
  1. Windows
Hi

I have the following code:

VBA Code:
Sub CreateQueries()
Application.ScreenUpdating = False
Set dict = CreateObject("scripting.dictionary")

For Each kom In ThisWorkbook.Worksheets("204B_Referenced").ListObjects("_204B_Referenced").ListColumns(6).DataBodyRange
   IsEmpty dict(kom.Value & kom.Offset(0, 6).Value)
Next

For i = 0 To dict.Count - 1
   With ThisWorkbook.Queries
      Name = "IT_Equpment_" & dict.Keys()(i)
      .Add Name:=Name, Formula:="let Source = ITSplitter(" & i & ") in Source"
   End With
   
   'Set sh = ThisWorkbook.Worksheets.Add(After:=ActiveSheet)
   Worksheets("template").Copy After:=ActiveSheet
   'Set sh = Sheets("Sheet4").Copy(After:=ActiveSheet)
   Set sh = ActiveSheet
   sh.Name = Name
       
   With sh.ListObjects.Add(SourceType:=0, Source:= _
      "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & Name _
      , Destination:=Range("$A$5")).QueryTable
      .CommandType = xlCmdSql
      .CommandText = Array("SELECT * FROM [" & Name & "]")
      .BackgroundQuery = False
      .RefreshStyle = xlOverwriteCells
      .AdjustColumnWidth = False
      .ListObject.DisplayName = Replace(Name, " ", "_", 1)
      .Refresh BackgroundQuery:=False
   End With
Next
Application.ScreenUpdating = True

End Sub

Im struggling with this line:
VBA Code:
Set sh = ThisWorkbook.Worksheets.Add(After:=ActiveSheet)
Which I wish to change for
VBA Code:
Set sh = Sheets("Sheet4").Copy(After:=ActiveSheet)

and for some reason that do not work.
Please help.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I have only seen that done in 2 steps.

VBA Code:
    Sheets("Sheet4").Copy After:=ActiveSheet
    Set sh = ActiveSheet
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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