Error in macro to make pivot tables

countingapples

Board Regular
Joined
Feb 20, 2003
Messages
243
I created a macro to automate the pivot table process, however, it hangs up when I try to run it.

Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!C1:C6").CreatePivotTable TableDestination:="", TableName:= _
        "WarehousePivot", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveSheet.PivotTables("WarehousePivot").PageFieldWrapCount = 1
    ActiveSheet.PivotTables("WarehousePivot").PivotCache.RefreshOnFileOpen = True
    
   With ActiveSheet.PivotTables("WarehousePivot").PivotFields("Acctg Date")  <---- This is where it highlights and hangs up.
        .Orientation = xlPageField
        .Position = 1
    End With
    
    With ActiveSheet.PivotTables("WarehousePivot").PivotFields("Prj/Grt")
        .Orientation = xlRowField
        .Position = 1
    End With
    
    With ActiveSheet.PivotTables("WarehousePivot").PivotFields("CC/ Org")
        .Orientation = xlColumnField
        .Position = 1
    End With
    
    ActiveSheet.PivotTables("WarehousePivot").AddDataField ActiveSheet.PivotTables( _
        "WarehousePivot").PivotFields("$ Amount"), "Count of $ Amount", xlCount
    
    ActiveWorkbook.ShowPivotTableFieldList = False
    
    Range("B5:J19").Select
       
    ActiveSheet.PivotTables("WarehousePivot").PivotFields("Count of $ Amount"). _
        Function = xlSum
    
    Range("B20:K20,K5:K19").Select
    
    Selection.Font.Bold = True
    
    Range("B5:K20").Select
    
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"

End Sub

When it hangs up, I receive the following error message:

Run time error '1004':

Unable to get the PivotFields property of the PivotTable Class.

What happened and how do I fix it? :rolleyes:
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,187,175
Messages
5,962,044
Members
438,578
Latest member
MrJimC

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