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?
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?