VBA Consolidating Multiple Sheets in a Pivot Table

c_clark

New Member
Joined
May 24, 2024
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi

I am new to the world of VBA and I found some code from another post that seemed to work perfectly for the post creator after community help. The code loops through all worksheets in the activeworkbook and consolidates these worksheets into one pivot table and creates the pivot in the active workbook on a new sheet. I am trying to get that to work for me but I keep running into a 'Run-time error 9' Subscript out of range for

arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"


I have 9 worksheets in this workbook (sheets 1 & 2 already contain data that gets dynamically updated from other worksheets as they are updated and I have another macro already written to produce a pivot table report for me on sheet 3.) I need to get a this code to produce one pivot table report based on the data in sheets 4-8 (labeled "HBB33 Report Period (x)" respectively and output the data on to sheet 9 labeled "AGG DATA REPORT."


Can anybody help me to modify the code? Thank you for any help you can provide. I am trying to learn by doing :).

Here is the full code:

Sub Create_PivotTable()
Dim i As Long
Dim arSQL() As String
Dim objPivotCache As PivotCache
Dim objRS As Object
Dim wks As Worksheet
Dim ws2 As Worksheet
With ActiveWorkbook
ReDim arSQL(4 To 12)
For Each wks In .Worksheets
Select Case wks.Name
Case "HBB33 REPORT PERIOD 1", "HBB33 REPORT PERIOD 2", "HBB33 REPORT PERIOD 3", "HBB33 REPORT PERIOD 4", "HBB33 REPORT PERIOD 5"
i = i + 1
arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"
End Select
Next wks
Set wks = Nothing
Set objRS = CreateObject("ADODB.Recordset")

objRS.Open Join$(arSQL, " UNION ALL "), _
Join$(Array("Provider=Microsoft.jet.OLEDB.4.0; Data Source=", _
.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)
Set objPivotCache = .PivotCaches.Add(xlExternal)
Set objPivotCache.Recordset = objRS
Set objRS = Nothing
End With
Set ws2 = Worksheets("AGG DATA REPORT")
With ws2
objPivotCache.CreatePivotTable TableDestination:=.Range("A3")
Set objPivotCache = Nothing
.Range("A3").Select
End With
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Sorry if I was unclear. I am trying to get this code to produce a pivot table based on the data in from sheets 4-8. This code is the code related to those sheets only.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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