Using If Else, on Pivot Tables

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
Solution

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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