Problem with on error goto, in pivot table macro - I still get an error

akzephyr

New Member
Joined
Dec 6, 2017
Messages
2
hi

I am trying to create an automated script to automatically change multiple examples of the same field dropped into a pivot table into defined calculations of that field. eg change the first instance into average, second into stdev, 3rd into count etc.

This requires them to be renamed to avoid an error, but I am trying to use the error as a tool to jump to a part of the code that will set them as a different calculation (and name).

This works until it tries convert the 2nd or 3rd example of the field, when it seems to ignore the on error goto instruction and throw up "Run-time error 1004 PivotTable field name already exists".

Can anyone help?

Rich (BB code):
Rich (BB code):
Sub SetPivotFieldsAutomatically()

   Dim pvtFld As Excel.PivotField
   Dim pt As PivotTable

    With Selection.PivotTable
        .ManualUpdate = True
        
For Each pvtFld In .DataFields
            With pvtFld
                On Error GoTo x1
                .Function = xlAverage
                .NumberFormat = "0.0"
                .Caption = "av." & pvtFld.SourceName
                GoTo x3

x1:
                On Error GoTo x2
                .Function = xlStDev
                .NumberFormat = "0.00"
                .Caption = "sd." & pvtFld.SourceName
                GoTo x3

x2:
.Function = xlcount
                .NumberFormat = "0"
                .Caption = "n." & pvtFld.SourceName

x3:
End With
    Next pvtFld
    End With

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
thank you fluff. I tried clearing the error with resume next statements, but then I ended up in a loop with the macro not completing.

Any ideas on getting through this?
 
Upvote 0
Try
Code:
x1:
on error goto -1
                On Error GoTo x2
                .Function = xlStDev
                .NumberFormat = "0.00"
                .Caption = "sd." & pvtFld.SourceName
                GoTo x3
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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