Macro to Loop Through All Sheets

sharpm80

New Member
Joined
Dec 1, 2017
Messages
8
Hi I have a data set which contains hundreds of cities and I want to run a pivot on each city and export each city's pivot to a new workbook. To accomplish this I ran a Macro to break each cities data out into its own sheet. Now for each city I would like to insert blank rows above the data and insert a pivot table into those blank rows. I have listed the code below to create the pivot but I am having trouble looping it through all the worksheets.



Sub Macro4()
'
' Macro4 Macro
'


Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String


Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown


'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & Range("A26:t9000").Address(ReferenceStyle:=xlR1C1)


'Where do you want Pivot Table to start?
StartPvt = ActiveSheet.Name & "!" & ActiveSheet.Range("A1").Address(ReferenceStyle:=xlR1C1)


'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)


'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable")


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi & welcome to the board.
Untested, but try
Code:
Sub Macro4()
'
' Macro4 Macro
'


    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String
    Dim Ws As Worksheet
    
    For Each Ws In Worksheets
        Ws.Rows(1).Resize(25).Insert
        
        'Determine the data range you want to pivot
        SrcData = Ws.Name & "!" & Ws.Range("A26:t9000").Address(ReferenceStyle:=xlR1C1)
        
        
        'Where do you want Pivot Table to start?
        StartPvt = Ws.Name & "!" & Ws.Range("A1").Address(ReferenceStyle:=xlR1C1)
        
        
        'Create Pivot Cache from Source Data
        Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=SrcData)
        
        
        'Create Pivot table from Pivot Cache
        Set pvt = pvtCache.CreatePivotTable( _
        TableDestination:=StartPvt, _
        TableName:="PivotTable")
    Next Ws


End Sub
 
Upvote 0
Hi Fluff, thank you for responding!

When I run the Macro with your updates I receive the following Run-time error '5' (Invalid procedure call or argument)
The following code is highlighted in the debug
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable")

Do you know why this run time error would occur?
 
Upvote 0
Unfortunately not.
I don't know anything about pivots & was showing you how you could loop through the worksheets.

Does it create any pivots, or does it crash on the first sheet?
 
Upvote 0
Okay gotcha Pivots can be tricky too because you have to make sure the data is set up the same way on every tab. And yes it crashes on the first sheet. The code I posted originally will produce a pivot though.
 
Upvote 0
Ok, once my macro inserts the blank rows, is the data in the correct place, or have I miscounted the number of rows to insert?
 
Upvote 0
It inserts the correct number of rows 25, and the data is where it should be because the range I have set starts at 26.
 
Upvote 0
OK, have a go with this
Code:
Sub Macro4()
'
' Macro4 Macro
'


    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String
    Dim Ws As Worksheet
    
    For Each Ws In Worksheets
        Ws.Activate
        Ws.Rows(1).Resize(25).Insert
        
        'Determine the data range you want to pivot
        SrcData = ActiveSheet.Name & "!" & Range("A26:t9000").Address(ReferenceStyle:=xlR1C1)
        
        
        'Where do you want Pivot Table to start?
        StartPvt = ActiveSheet.Name & "!" & ActiveSheet.Range("A1").Address(ReferenceStyle:=xlR1C1)
        
        
        'Create Pivot Cache from Source Data
        Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=SrcData)
        
        
        'Create Pivot table from Pivot Cache
        Set pvt = pvtCache.CreatePivotTable( _
        TableDestination:=StartPvt, _
        TableName:="PivotTable")
    Next Ws


End Sub
 
Upvote 0
I received the same error message unfortunately. That was a good try switching back to Activesheet.
 
Upvote 0
Also, I just tried to run my original code and it is not producing the pivot anymore, it is now also producing same error after creating rows. I will play with it to get it back to producing a pivot.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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