VBA with pivot table - reposting. please help!

nancymk

Board Regular
Joined
Jul 24, 2008
Messages
106
For a certain field in my pivot table (lets call it field1) I want to show only choices 1-5 and remove blanks and anything else that pops up. The problem is, there are a lot of other things that could be in field1, so it's not has easy as turning off the visibility for blanks. Also, there might not be 1-5 every time.

How would I change this code to turn visibility for all other choices to false and not crash if say there is no 2.

Sheets("Com").Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Field1")
.PivotItems("1").Visible = True
.PivotItems("2").Visible = True
.PivotItems("3").Visible = True
.PivotItems("4").Visible = True
.PivotItems("5").Visible = True
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
    Dim pt As PivotTable
    Dim pi As PivotItem
    Set pt = ActiveSheet.PivotTables("PivotTable2")
    pt.ManualUpdate = True
    For Each pi In pt.PivotFields("Field1").PivotItems
        Select Case pi.Value
            Case "1", "2", "3", "4", "5"
                pi.Visible = True
            Case Else
                pi.Visible = False
        End Select
    Next pi
    pt.ManualUpdate = False
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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