Pivot Table Macro

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
150
Office Version
  1. 365
Platform
  1. Windows
I am having some trouble with my pivot table VBA. On this line, I get an error

"ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
"pTable1").PivotFields("REGULAR EARNINGS"), "Sum of REGULAR EARNINGS", _
xlSum"

I figure I need a With statement, but am unsure of what that should be. Any ideas?

Chad


VBA Code:
    Dim rng1 As Range
    Dim sht1 As Worksheet
    Dim pTable1 As PivotTable
    Set rng1 = ActiveSheet.Cells(1, 1).CurrentRegion
    Set sht1 = Sheets("FLS Pivot Table")
    Set pTable1 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        rng1.Address, Version:=8).CreatePivotTable(TableDestination:= _
        sht1.Cells(1, 1), TableName:="pTable1" & Format(Time, "hhmmss"))
    Sheets("FLS Pivot Table").Select
    Cells(2, 2).Select
    With pTable1
        With .PivotCache
            .RefreshOnFileOpen = False
            .MissingItemsLimit = xlMissingItemsDefault
        End With
        With .PivotFields("COMPANY CODE")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("NAME")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("QTR")
            .Orientation = xlColumnField
            .Position = 1
        End With
        ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
            "pTable1").PivotFields("REGULAR EARNINGS"), "Sum of REGULAR EARNINGS", _
            xlSum
        ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
            "pTable1").PivotFields("OVERTIME EARNINGS"), "Sum of OVERTIME EARNINGS", _
            xlSum
        ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
            "pTable1").PivotFields("Regular"), "Sum of Regular", xlSum
        ActiveSheet.PivotTables("pTable1").PivotFields("Sum of REGULAR EARNINGS"). _
            Orientation = xlHidden
        ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
            "pTable1").PivotFields("Overtime"), "Sum of Overtime", xlSum
        ActiveSheet.PivotTables("pTable1").AddDataField ActiveSheet.PivotTables( _
            "pTable1").PivotFields("Gross Earnings"), "Sum of Gross Earnings", xlSum
        ActiveSheet.PivotTables("pTable1").PivotFields("Sum of OVERTIME EARNINGS"). _
            Orientation = xlHidden
        ActiveSheet.PivotTables("pTable1").PivotFields("COMPANY CODE"). _
            ClearAllFilters
        ActiveSheet.PivotTables("pTable1").PivotFields("COMPANY CODE").CurrentPage _
            = "FLS"
    End With
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I solved most of it, but am now stuck on the very last line of code.

VBA Code:
Dim rng1 As Range
    Dim sht1 As Worksheet
    Dim pTable1 As PivotTable
    Set rng1 = ActiveSheet.Cells(1, 1).CurrentRegion
    Set sht1 = Sheets("FLS Pivot Table")
    Set pTable1 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        rng1.Address, Version:=8).CreatePivotTable(TableDestination:= _
        sht1.Cells(1, 1), TableName:="pTable1" & Format(Time, "hhmmss"))
    Sheets("FLS Pivot Table").Select
    Cells(2, 2).Select
    With pTable1
        With .PivotCache
            .RefreshOnFileOpen = False
            .MissingItemsLimit = xlMissingItemsDefault
        End With
        With .PivotFields("COMPANY CODE")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("NAME")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("QTR")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Regular")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        With .PivotFields("Overtime")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        With .PivotFields("Gross Earnings")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        ActiveSheet.PivotTables("pTable1").PivotFields("COMPANY CODE").CurrentPage _
            = "FLS"
 
Upvote 0
Got it. Finally.

Code:
    Dim rng1 As Range
    Dim sht1 As Worksheet
    Dim pTable1 As PivotTable
    Set rng1 = ActiveSheet.Cells(1, 1).CurrentRegion
    Set sht1 = Sheets("FLS Pivot Table")
    Set pTable1 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        rng1.Address, Version:=8).CreatePivotTable(TableDestination:= _
        sht1.Cells(1, 1), TableName:="pTable1" & Format(Time, "hhmmss"))
    Sheets("FLS Pivot Table").Select
    Cells(2, 2).Select
    With pTable1
        With .PivotCache
            .RefreshOnFileOpen = False
            .MissingItemsLimit = xlMissingItemsDefault
        End With
        With .PivotFields("COMPANY CODE")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("NAME")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("QTR")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Regular")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        With .PivotFields("Overtime")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        With .PivotFields("Gross Earnings")
            .Orientation = xlDataField
            .Function = xlSum
        End With
        With .PivotFields("COMPANY CODE")
            .CurrentPage = "FLS"
        End With
    End With
 
Upvote 0
Solution

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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