1

I am trying to set all items in a pivot table fields visible = false. E.g I have 5 divisions in each country called DivA, DivB, DivC, DivD and DivE. Now sometimes in my source data i have more or less than the above mentioned division so I want to turn all otions off under pivotfield"Division" and then make visible all the above mentioned divisions if they are there (sometimes there might be 3, 4 or all 5 divisions).

I found a code online and trying to incorporate it but it keeps giving me error of setting "False = False".

Any Help would be much appreciated!

Please see my code below:

Sub test()
'
' test Macro
'


   With ActiveSheet.PivotTables("PivotTable3").PivotFields("Division")

    Dim Table As PivotTable
    Dim FoundCell As Object
    Dim All As Range
    Dim PvI As PivotItem

    Set All = Worksheets("Sheet1").Range("A7:AZ10000")
    Set Table = Worksheets("Sheet1").PivotTables("PivotTable3")
    For Each PvI In Table.PivotFields("Division").PivotItems
        Set FoundCell = All.Find(PvI.Name)
        If FoundCell <> "itemname" Then
            PvI.Visible = False
        End If
    Next

        .PivotItems("DivA").Visible = True
        .PivotItems("DivB").Visible = True
        .PivotItems("DivC").Visible = True
        .PivotItems("DivD").Visible = True
        .PivotItems("DivE").Visible = True


    End With
End Sub

2 Answers 2

2

Is this what you are trying? (UNTESTED)

Sub test()
    Dim table As PivotTable
    Dim PvI As PivotItem

    Set table = Worksheets("Sheet1").PivotTables("PivotTable3")

    With table.PivotFields("Division")
        For Each PvI In .PivotItems
            Select Case PvI.Name
            Case "DivA", "DivB", "DivC", "DivD", "DivE"
                PvI.Visible = True
            Case Else
                PvI.Visible = False
            End Select
        Next
    End With
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Sid! this is exactly what i was trying to do!
0

I tested the answer macro...it works.

Sub Pivot_Test()

Sheets("Macro").Select
Dim Dt As String
Dt = Range("C1")

Dim PMth1 As String
Dim PMth2 As String
Dim PMth3 As String
Dim PMth4 As String
Dim PMth5 As String
Dim PMth6 As String
Dim PMth7 As String
Dim PMth8 As String
Dim PMth9 As String
Dim PMth10 As String
Dim PMth11 As String
Dim PMth12 As String

PMth1 = Range("J1")
PMth2 = Range("J2")
PMth3 = Range("J3")
PMth4 = Range("J4")
PMth5 = Range("J5")
PMth6 = Range("J6")
PMth7 = Range("J7")
PMth8 = Range("J8")
PMth9 = Range("J9")
PMth10 = Range("J10")
PMth11 = Range("J11")
PMth12 = Range("J12")




Sheets("MANCO").Select

Dim pt1 As PivotTable
Dim pt2 As PivotTable
Dim PI1 As PivotItem
Dim PI2 As PivotItem

Set pt1 = Worksheets("MANCO").PivotTables("Resolution")

With pt1.PivotFields("Month")
    For Each PI1 In .PivotItems
        Select Case PvI.Name
        Case PMth1, PMth2, PMth3, PMth4, PMth5, PMth6, PMth7, PMth8, PMth9, PMth10, PMth11, PMth12
            PI1.Visible = True
        Case Else
            PI1.Visible = False
        End Select
    Next
End With

Set pt2 = Worksheets("MANCO").PivotTables("Complaints")


With pt2.PivotFields("Month")
    For Each PI2 In .PivotItems
        Select Case PI2.Name
        Case PMth1, PMth2, PMth3, PMth4, PMth5, PMth6, PMth7, PMth8, PMth9, PMth10, PMth11, PMth12
            PI2.Visible = True
        Case Else
            PI2.Visible = False
        End Select
    Next
End With

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.