pt.GetPivotData().value question

winglessbuzzard

New Member
Joined
Jan 12, 2009
Messages
29
I'm having a problem combining variables in a string and using that string for the formula in a GetPivotData VBA (=GetPivotData() in a cell) formula. I'm trying to keep it out of excel to avoid excel's application recalculation time.


I get an Application-defined or Object-defined error on this line
<code>
If IsError(cutPT.GetPivotData(NewFrmla).Value) = True Then
c.Value = 0
End If
</code>

in this subroutine
<code>
Sub RunCut(ByVal cutPT As PivotTable, ByVal cWSN As String)
Dim rPLT As Range, i As Integer
Dim cNM As String, cRNG As Range, c As Range, tws As Worksheet
Dim Frmla As String, NewFrmla As String, RevEndRow As Integer, StatsBegRow As Integer
Dim ws As Worksheet, ordr As Integer



Set rPLT = ThisWorkbook.Worksheets("CutSpammer").Range("L3")
Set tws = ThisWorkbook.Worksheets("CutTemplate")

Frmla = tws.Range("I1").Formula


RevEndRow = 23
StatsBegRow = 415

i = 0
Do While Len(rPLT.Offset(i, 0).Formula) > 0
cNM = rPLT.Offset(i, 1).Value
Set cRNG = tws.Range(rPLT.Offset(i, 2).Value)

For Each c In cRNG
If Len(tws.Range("J" & c.Row).Formula) > 2 Then
If c.Offset(-c.Row + 1, 0).Value <> "" And c.Offset(-c.Row + 2, 0).Value <> "" Then
NewFrmla = Replace(Frmla, ",1,", "," & Month(c.Offset(-c.Row + 2, 0).Value) & ",")
NewFrmla = Replace(NewFrmla, "400000_Food", tws.Range("K" & c.Row).Value)
NewFrmla = Replace(NewFrmla, "Material Costs - Standard Material Costs", tws.Range("G" & c.Row).Value)
NewFrmla = Replace(NewFrmla, " BGT_2012", cNM)
If IsError(cutPT.GetPivotData(NewFrmla).Value) = True Then
c.Value = 0
Else
If c.Row < RevEndRow Or c.Row > StatsBegRow Then
c.Value = cutPT.GetPivotData(NewFrmla).Value
Else
c.Value = -1 * cutPT.GetPivotData(NewFrmla).Value
End If
End If
End If
End If
Next c
i = i + 1
Loop

End Sub
</code>

The exact string in the cell tws.Range("I1").Formula is " BGT_2012", "MN", "1", "T5", "Material Costs - Standard Material Costs", "PL_Metric", "400000_Food"

If I paste this string in place of NewFrmla in the line IsError(cutPT.GetPivotData(NewFrmla).Value), then it works fine.

If I paste the string created by the code up to the above line into the line above, it works fine. Is VBA dropping the quotes off somewhere?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

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