Create Pivot Table in VBA Error 1004 Unable to get the Pivot Tables Property

mheilberg

New Member
Joined
Jun 2, 2014
Messages
3
Excel 2010
Windows 2007
I have checked all other posts with the same issue and cannot find and example that matches mine.

I am creating a pivot table in VBA. I am getting the error message:
Runtime error 1004-Unable to get the Pivot Tables property
I have checked that the pivot table name is correct and it exists in the current focus\named worksheet.

I would appreciate it if anyone has the time to take a look at my code, and let me know if the problem is evident. Thanks for your help!

The error pops up when I begin assigning fields to the pivot table (labeled below)

Rich (BB code):
Sub pivot()
Dim strSheetName As String     'Source Data Sheet
Dim strCurrentRegion As String  'Source Data Range
Dim strSourceData As String     'Source Sheet &  "!" & Source Range

Dim PTCache As PivotCache
Dim PT As PivotTable
Dim strPivotTableName As String 'Pivot Table Dynamic Name
    
    
    
    strPivotTableName = "PT " & Replace(Now(), "/", "_")
    strPivotTableName = Replace(strPivotTableName, ":", "-")
    lExcelVersion = Application.Version
    strSheetName = ActiveSheet.Name
    
   
    Selection.CurrentRegion.Select
    
    strCurrentRegion = Selection.Address
    strSourceData = strSheetName & "!" & strCurrentRegion
    
    Set PTCache = ActiveWorkbook.PivotCaches.Add _
        (SourceType:=xlDatabase, _
        SourceData:=strSourceData)
        
    Set PT = PTCache.CreatePivotTable _
        (TableDestination:="", _
        TableName:=strPivotTableName)


'========================================================================================
'At the following WITH clause, Runtime error 1004-Unable to get the Pivot Tables property
'When I comment out the WITH clause, the error pops up for the next "ActiveSheet.PivotTables(PT) clause.
'========================================================================================

    With ActiveSheet.PivotTables(PT).PivotFields( _
        "Assigned To")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Opened On"), _
        "Sum of Opened On", xlSum
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Opened in Period"), _
        "Sum of Opened in Period", xlSum
    With ActiveSheet.PivotTables(PT).DataPivotField
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Closed in Period"), _
        "Sum of Closed in Period", xlSum
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Left Open On"), _
        "Sum of Left Open On", xlSum
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Total Opened in Period"), _
        "Sum of Total Opened in Period", xlSum
    ActiveSheet.PivotTables(PT).PivotFields( _
        "Sum of Opened On").Caption = "Opened at Start"
    ActiveSheet.PivotTables(PT).PivotFields( _
        "Sum of Opened in Period").Caption = "Opened during Period"
    ActiveSheet.PivotTables(PT).PivotFields( _
        "Sum of Closed in Period").Caption = "Closed during Period"
    ActiveSheet.PivotTables(PT).PivotFields( _
        "Sum of Left Open On").Caption = "Open at end of Period"
End Sub


 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to MrExcel.

PT is an object variable so in place of:

Code:
With ActiveSheet.PivotTables(PT)

you can use:

Code:
With PT
 
Upvote 0
Thank you very much for your fast response. I made the change and it worked for the first clause.
If I may trouble you further, now that i make it past the Assigned To field, I error out on the next clause
Code:
  ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Opened On"), _
        "Sum of Opened On", xlSum
Before bothering you, I did try rewriting the code following your earlier example, with no luck.
Any advice?
 
Upvote 0
Did you replace all occurrences of ActiveSheet.PivotTables(PT) with PT?

Yes I did. I received the following error
Error 438 Object doesn't support this property or method

original as recorded by Macro
Code:
    ActiveSheet.PivotTables(PT).AddDataField ActiveSheet. _
        PivotTables(PT).PivotFields("Opened On"), _
        "Sum of Opened On", xlSum

My changes
Code:
    PT.AddDataField ActiveSheet. _
        PT.PivotFields("Opened On"), _
        "Sum of Opened On", xlSum

I have written vba for creating Pivot Tables, but not like this table (Multiple aggregates in the datafield, no data columns) Obviously, I have discovered my ignorance regarding this particular syntax. Thank you for your continued advice.
Do you know of a comprehensive resource regarding PivotTables\vba?
 
Upvote 0
You don't need ActiveSheet.. Remember that PT is an object variable. It's not a string and it doesn't need to be qualified.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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