Looping Through Workbook and making pivot tables on each worksheet

rzacek87

New Member
Joined
Jun 4, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have looked at other solutions and am close to having code working. However the code does not loop to the next sheet:

VBA Code:
Sub CreateAPivotTable()


    Dim shtSource As Worksheet
    Dim rngSource As Range, rngDest As Range
    Dim pvt As PivotTable

    For Each shtSource In ActiveWorkbook.Worksheets
        If shtSource.Name <> "Original" Then

        On Error GoTo ErrHandler


        Application.ScreenUpdating = False



        Set shtSource = ActiveSheet


        Set rngSource = shtSource.Range("A1").CurrentRegion


        Set rngDest = ActiveSheet.Range("P1")


        Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
        Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, DefaultVersion:=xlPivotTableVersion12)

        pvt.AddDataField pvt.PivotFields("Policy #"), "Count of Policy #", xlCount


        With pvt.PivotFields("Policy #")
            .Orientation = xlRowField
            .Position = 1
        End With


        pvt.TableStyle2 = "PivotStyleDark7"
        With Cells.Font
            .Name = "Calibri"
            .Size = 8
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With

        ActiveWorkbook.ShowPivotTableFieldList = False
  
        End If
    Next shtSource


    Application.ScreenUpdating = True

    Exit Sub

    ErrHandler:
    Application.ScreenUpdating = True
    MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"


End Sub
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Remove:

VBA Code:
Set shtSource = ActiveSheet
 
Upvote 0
If the pivots are supposed to be on each sheet too, then this:

VBA Code:
Set rngDest = ActiveSheet.Range("P1")

should be:

VBA Code:
Set rngDest = shtSource.Range("P1")
 
Upvote 0
Solution
If the pivots are supposed to be on each sheet too, then this:

VBA Code:
Set rngDest = ActiveSheet.Range("P1")

should be:

VBA Code:
Set rngDest = shtSource.Range("P1")
That took care of both issues now.
Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
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