Using If Else, on Pivot Tables

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
566
Office Version
  1. 2019
Platform
  1. Windows
hi guys-

I built a macro where i move pivot table columns around as one of the last steps. (First time automating a pivot table).

The report has financial info, so like "Current", "120+", "180+"

however, if the data doesnt have one of those statuses, the Macro errors out because it cant find that pivot table field. How can i work around and have the macro skip over if that status is not available?

VBA Code:
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Pro Num"), "Count of Pro Num", xlCount
    Range("H2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "Current").Position = 1
    Range("C2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "120+").Position = 7
    Range("D2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "180+").Position = 7
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,951
Have you tried adding "On Error Resume Next"?

VBA Code:
On Error Resume Next
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Pro Num"), "Count of Pro Num", xlCount
    Range("H2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "Current").Position = 1
    Range("C2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "120+").Position = 7
    Range("D2").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Age Bucket").PivotItems( _
        "180+").Position = 7
 
Solution

Forum statistics

Threads
1,143,839
Messages
5,721,093
Members
422,339
Latest member
SHIVATVM

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
Top