Pivot Table in Macro Not Working

aj22

New Member
Joined
Jun 27, 2007
Messages
19
When I run the following macro, it comes back with "unable to get the pivot fields property of the pivot table class". Any ideas? Debugger hi-lights:

Rich (BB code):
    Cells.Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'1_HC_INFO'!C1:C16").CreatePivotTable TableDestination:="", TableName:= _
        "PivotTable5", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
        "PivotTable5").PivotFields("Headcount"), "Count of Headcount", xlCount    ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of Headcount"). _
        Function = xlSum
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("BusVal_1")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Bus_val 2")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Class")
        .Orientation = xlRowField
        .Position = 3
    End With
    Sheets("Sheet8").Select
    Sheets("Sheet8").Name = "OnePager_Pivot"
    Sheets("OnePager").Select
    Cells.Replace What:="#REF!", Replacement:="OnePager_Pivot!$A$3", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, _
        ReplaceFormat:=False
    Cells.Replace What:="#REF!", Replacement:="OnePager_Pivot!", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, _
        ReplaceFormat:=False
End Sub


Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

almagg

Well-known Member
Joined
Aug 21, 2002
Messages
705
i had to work with pivot tables a couple months ago and here is some of the code:

'this first part creates the pivot table:

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= "DataTable").CreatePivotTable TableDestination:= _
"Pivots!R4C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

in yours you have your "Destination" argument on another line.
try putting it in using the Sheet Name and R1C1 notation.

in particular maybe get rid of this line in your code:

ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select

the rest looks similar to what i ended up with.

one of my data fields:

ActiveSheet.PivotTables("PivotTable1").AddDataField _
ActiveSheet.PivotTables("PivotTable1").PivotFields("Capital Cost _
Estimate"),"Sum of Capital Cost Estimate", xlSum

a couple row fields:

With ActiveSheet.PivotTables("PivotTable1")
.PivotFields("ProjectId").Orientation = xlRowField
.PivotFields("ProjectId").Position = 1
.PivotFields("ProjectId").Subtotals = Array(False, False, False, False,
False, False, False, False, False, False, False, False)

.PivotFields("Unit").Orientation = xlRowField
.PivotFields("Unit").Position = 2
.PivotFields("Unit").Subtotals = Array(False, False, False, False, False,
False, False, False, False, False, False, False)
End With
 

Forum statistics

Threads
1,181,102
Messages
5,928,062
Members
436,586
Latest member
latintxn

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
Top