VBA Code for Excel Power Query - change

Radoslaw85

New Member
Joined
Aug 13, 2021
Messages
8
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,255
Office Version
  1. 365
Platform
  1. Windows
I have only seen that done in 2 steps.

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

Forum statistics

Threads
1,147,731
Messages
5,742,852
Members
423,758
Latest member
accountfornada

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
Top