VBA Consolidating Multiple Sheets in a Pivot Table

ThomasB

Active Member
Joined
May 2, 2007
Messages
314
Hi

I got a brilliant bit of code (which works perfectly) from Bill Jelens "Excel Gurus gone Wild" which loops through all worksheets in the activeworkbook and consolidates these worksheets into one pivot table, this example creates the pivot in a new workbook, what I would really like to do is add a new sheet and create the pivot in the active workbook where I am pulling the data from can anybody help me to modify the code?

I have not added the code yet to generate the pivot

Any help would be greatly appreciated

Kind Regards

Thomas

Code below:

Option Explicit
Sub bob()

Dim i As Long
Dim arSQL() As String
Dim objPivotCache As PivotCache
Dim objRS As Object
Dim wbkNew As Workbook
Dim wks As Worksheet

With ActiveWorkbook
ReDim arSQL(1 To .Worksheets.Count)
For Each wks In .Worksheets
Do Until wks.Name = ""
i = i + 1
arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"
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)
End With

Set wbkNew = Workbooks.Add(Template:=xlWBATWorksheet)

With wbkNew
Set objPivotCache = .PivotCaches.Add(xlExternal)
Set objPivotCache.Recordset = objRS
Set objRS = Nothing

With .Worksheets(1)
objPivotCache.CreatePivotTable TableDestination:=Range("A3")
Set objPivotCache = Nothing
Range("A3").Select
End With
End With
Set wbkNew = Nothing
End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This is untested but it compiles :)

Code:
Sub bob()
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(1 To .Worksheets.Count)
    For Each wks In .Worksheets
        i = i + 1
        arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"
    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.Add
With ws2
    objPivotCache.CreatePivotTable TableDestination:=.Range("A3")
    Set objPivotCache = Nothing
    .Range("A3").Select
End With
End Sub
 
Upvote 0
Hi Peter

Your code works a treat, thank you very much for your help much appreciated

Kind Regards

Thomas
 
Upvote 0
Hi Peter

I have a one more small question, I have a large number of worksheet in the workbook I would only like to use the sheets entitled with the month names ie Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec to create the pivot table

Have tried the following code:

For Each wks In .Worksheets
Select case wks.Name
Case wks.Name = "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
i = i + 1
arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"
End Select
Next wks

Unfortunaltely it throws up the following error "Run-Time-Error Syntax Error in query. Incomplete query clause", have you any idea what I am doing wrong?

Kind Regards

Thomas
 
Upvote 0
I think there's an error in your Select Case syntax - try

Code:
For Each wks In .Worksheets
    Select Case wks.Name
        Case "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            i = i + 1
            arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"
    End Select
Next wks
 
Upvote 0
Hi Peter

Thanks awfully for your help

I used your code, but unfortunatley it still throws up the same error, I steped through the code and the select case statement works perfectly the error occurs when I get to

objRS.Open Join$(arSQL, " UNION ALL "), _
Join$(Array("Provider=Microsoft.jet.OLEDB.4.0; Data Source=", _
.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)

which I find most strange as it should work!!

Kind Regards

Thomas
 
Upvote 0
Managed to work it out

needed to change this line of code:

ReDim arSQL(1 To .Worksheets.Count)

To

ReDim arSQL(1 To 12)

Then works a treat

The boss is going to be most impressed on Monday your help was greatly appreciated thank you

Kind Regards

Thomas
 
Upvote 0
I have similar situation where I am creating a Pivot form multiple worksheets. The pivot also is created as a separate sheet in the same file as other worksheets.Now how do I automatically refresh the Pivot table if any of the source worksheets are updated. Somehow I am not able to do it manually wither since the 'Refresh data' menu is greyed out. I would prefer auto refresh.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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