VBA to skip pivot filter based on If statement

gmittar

Board Regular
Joined
Sep 16, 2013
Messages
62
Hi All,

I have a nice macro that I use to loop through pivot items and carry out a task for each item in the pivot table (code below). It works well, and right now it loops through all items in the pivot table.

I would like to add an if statement that if True skips the code and goes to the next pivot item. I’ve highlighted my attempt at this below, but it looks like I can’t nest For/Next statements, which is key functionality in the pivot filter loop.

Is there another way that I can accomplish this?

As always thanks so much for your help.

Rich (BB code):
Dim ExternalLinks As Variant
Dim x As Long
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
Dim ILoop As Long
Dim shape As Excel.shape
 
 
    Application.ScreenUpdating = False
   
  
   
 
Set pt = Sheets("sheetnameiswhat").PivotTables(1)
Set pf = pt.PageFields(1)
 
'cycles through pivot table filter and carries out the below
For Each pi In pf.PivotItems
    pf.CurrentPage = pi.Value
   
'if cell(i51) is zero, skip macro and go directly to next pi   
If Range("i51") = 0 Then Next pi
 
End If
   
   
‘’ FUN CODE HERE''
 
  
Next pi
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try:

Code:
Set pt = Sheets("sheetnameiswhat").PivotTables(1)
Set pf = pt.PageFields(1)
 
'cycles through pivot table filter and carries out the below
For Each pi In pf.PivotItems
    pf.CurrentPage = pi.Value


    If Range("i51") <> 0 Then
       '[COLOR=#333333]FUN CODE HERE[/COLOR] 
    End If
Next pi
 
Upvote 0
Thanks Dante, that did it! Additionally, the shift in perspective from "don't run if" to "only run if" is helpful going forward.

Appreciate the help.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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