VBA - Uncheck dynamic range in Pivot Field

Goose306

Board Regular
Joined
Sep 26, 2014
Messages
52
Hi all,

Got a question, should be relatively straightforward but I'm struggling this morning. I have a macro that runs through reports that are generated by SAS and automatically manipulates them based on parameters set by a user.
  • There are new variables that have come up that need to be automatically excluded in most situations. The issue is, this variable goes under several names that is regularly changing.
  • To make it easier to maintain, I've decided to make a list of these variables in the master file where users set parameters so these can be accounted for.

I haven't previously worked with looping through a dynamic range to set pivot items/remove pivot items, so I'm unsure on how to best proceed. Here's a snippet of current code that is currently not working with a nested To/For Loop:

Code:
  Public va2 As Worksheet     'Validation 2 tab
  Public ap As Worksheet      'Audit Parameters tab
  Public cel As Range         'Cell reference for loops
  Set ap = ThisWorkbook.Worksheets("Audit Parameters")  Set va2 = ThisWorkbook.Worksheets("VALIDATION2")
  Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
  Dim pi As PivotItem
  Dim varx As Range
    Set varx = va2.Range("M2", Range("M2").End(xlDown))

'Handle VarX
  If ap.Cells(9, 6) <> "" Then
    For Each pi In pt.PivotFields("Skill").PivotItems
      For Each cel In varx
        If pi.Value = cel Then
          pi.Visible = True
        Else
          pi.Visible = False
        End If
      Next cel
    Next pi
  End If

Any help is appreciated, thanks!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Bump. Any help is appreciated. I've also tried to change handling to running off a loop using an integer but no luck.

Code:
  'Handle VarX  If ap.Cells(9, 6) <> "" Then
    For i = 1 To varx.Count
      For Each pi In pt.PivotFields("Skill").PivotItems
        If pi.Value = varx.Cells(i).Value Then
          pi.Visible = False
        Else
          pi.Visible = True
        End If
      Next pi
    Next i
  End If
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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