CalculatedFields.add function not working when refreshed...

D

Deegan

I am updating a pivot table based on the users input from a drop down box.

The rows will become one of four different options based on that input, but
I'd also like to add a RANK to their return - which is a running total
calculated field.

When I run the calculated field the first time it works fine, however, when
I refresh, it seems to want to add it again and it gives me an error. It
won't let me reference the calculated field like it does the real fields
either.

I'm stuck, can ya' help?

J

Public Sub changepivot()

Dim P As PivotTable
Set P = Worksheets("Capital").PivotTables("FakePivot")
Set PRegion = P.PivotFields("Region")
Set PState = P.PivotFields("State")
Set PTotal1 = P.PivotFields("Total1")
Set Prank = P.CalculatedFields("Rank")

If Range("Selected").Value = "All" Then
With P
PRegion.Orientation = xlHidden
PState.Orientation = xlHidden
End With
ElseIf Range("Selected").Value = "Gulf" Or Range("Selected").Value =
"Atlantic" Or Range("Selected").Value = "North" Or Range("Selected").Value =
"Florida" Then
With P
With PRegion
.Orientation = xlRowField
.AutoSort xlDescending, "Sum of Total1"
.Position = 1
End With
PState.Orientation = xlHidden
.CalculatedFields.Add "Rankb", "=1" 'THIS IS WHERE IT ERRORS
End With
Else
With P
With PState
.Orientation = xlRowField
.AutoSort xlDescending, "Sum of Total1"
.Position = 1
End With
PRegion.Orientation = xlHidden
End With
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top