Using GetPivotData in VBA

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I'm trying to use this worksheetfunction in a Macro w/no luck...any ideas?
(I initially recorded a macro to get the correct "GetPivotData" elements and altered)

here is the partial/relevant code
Code:
Dim Temp as Variant
.
.
For i = 6 To LR
    If Range("C" & i) <> "" Then
        Temp = WorksheetFunction.GetPivotData("Counts", Sheets(2).Range("A7"), "Year Resolved", Range("B" & i), "Year UW", Range("C" & i))
            If Not IsNumeric(Temp) Then
                Range("E" & i) = 0
            Else
                Range("E" & i) = Temp
            End If
    End If
Next i
My code stops at the Temp = line...and says "Object doesn't support this property or method"
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You need to apply it to your pivottable, eg ActiveSheet.PivotTables(1). Have a look at the example in VBA Help.
 
Upvote 0
From looking at the Help, I added/changed the following...

Code:
Dim PT As PivotTable
    Set PT = Sheets("Access Data").PivotTables("Access_Pivot")
.
.
.
Temp = PT.GetPivotData("Counts", "Year Resolved", Sheets("Report").Range("B" & i), "Year UW", Sheets("Report").Range("C" & i))

It works, until I hit an instance were the Values in B & C don't apply. I was hoping that the following code would help me fill a zero, but the code does not fill "Temp" with an error.

Code:
            If Not IsNumeric(Temp) Then
                Range("E" & i) = 0
            Else
                Range("E" & i) = Temp
 
Upvote 0
...or is it possible to change the pivot table to show all the parameters, even if the results are zero?
 
Upvote 0
What is assigned to Temp when the values in B & C don't apply?

I guess "Don't Apply" isn't the right term.

Example:

Sheet1, Column A contains years 1999 - 2011
Sheet2, pivot table data: depending on the filter(s), some years will have no applicable data and those years do not appear in the Pivot.

Then when going thru the code and trying to look for a non-existent year in the pivot table, the code crashes with the Application-defined or object-defined error.
 
Upvote 0
You need an error trap:

Code:
    Dim PT As PivotTable
    Set PT = Sheets("Access Data").PivotTables("Access_Pivot")
'   Start of loop
    On Error Resume Next
    Temp = PT.GetPivotData("Counts", "Year Resolved", Sheets("Report").Range("B" & i), "Year UW", Sheets("Report").Range("C" & i))
    If Err = 0 Then
        Range("E" & i) = Temp
    Else
        Err.Clear
        Range("E" & i) = Temp
    End If
    On Error GoTo 0
'   End of loop
 
Upvote 0
You need an error trap:

Rich (BB code):
    Dim PT As PivotTable
    Set PT = Sheets("Access Data").PivotTables("Access_Pivot")
'   Start of loop
    On Error Resume Next
    Temp = PT.GetPivotData("Counts", "Year Resolved", Sheets("Report").Range("B" & i), "Year UW", Sheets("Report").Range("C" & i))
    If Err = 0 Then
        Range("E" & i) = Temp
    Else
        Err.Clear
        Range("E" & i) = 0
    End If
    On Error GoTo 0
'   End of loop

Thanks!

I did have to make 1 slight change. ;)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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