VBA code to help split PQ result

Radoslaw Poprawski

Active Member
Joined
Jun 19, 2021
Messages
405
Office Version
  1. 365
Platform
  1. Windows
I have PQ query that works with VBA.

Currently this code:

VBA Code:
Sub CreateQueries()

Set dict = CreateObject("scripting.dictionary")

For Each kom In ThisWorkbook.Worksheets("Base_Data").ListObjects("Table1").ListColumns(1).DataBodyRange
   IsEmpty dict(kom.Value & kom.Offset(0, 1).Value)
Next

For i = 0 To dict.Count - 1
   With ThisWorkbook.Queries
      nazwa = "CostCenter_" & i
      .Add Name:=nazwa, Formula:="let Zródlo = funkcja(" & i & ") in Zródlo"
   End With
   
   Set sh = ThisWorkbook.Worksheets.Add(After:=ActiveSheet)
   sh.Name = nazwa
       
   With sh.ListObjects.Add(SourceType:=0, Source:= _
      "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & nazwa _
      , Destination:=Range("$A$1")).QueryTable
      .CommandType = xlCmdSql
      .CommandText = Array("SELECT * FROM [" & nazwa & "]")
      .BackgroundQuery = False
      .RefreshStyle = xlOverwriteCells
      .AdjustColumnWidth = False
      .ListObject.DisplayName = nazwa
      .Refresh BackgroundQuery:=False
   End With
Next

End Sub

Currently, it splits into different sheets by cost center.

I have no idea how to make this code split the result into workbooks, where each workbook is one location with all Cost centers sheets assigned to that location.

The expected result is this: Each file is named after the location. in each file, I should get separate sheets with cost centers that belong to it.

Here is a link to the whole file: custom_Function_PQ.xlsm
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,226,737
Messages
6,192,737
Members
453,753
Latest member
SORD

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