Home » excel » excel – Is there a way to uncheck other items in a pivot table except a specific one?

excel – Is there a way to uncheck other items in a pivot table except a specific one?

Posted by: admin May 14, 2020 Leave a comment


I am new to VBA and I am having some trouble with some code on how to uncheck or deselect all items in a pivot table except a specific one.

I am creating a button to automatically filter data that will only reveal data in January (“20190131”) or in February (“20190228”) and so on etc.

I still dont have data for October (“20191031”), November and December, which causes error “Unable to set the Visible property of the PivotItem” because there is still no existing data of those dates. I just want an item to be checked and the rest to be unchecked.

I tried manually setting the visibility of Pivot Items to false and it is taking a long time for excel to uncheck them and I do not want my items to be static. And also if data still is not existing for example the data I am using is only from January 2019 to September 2019.

   Sub Procdate_January()

  Procdate_January Macro
  ' Macro for changing the PROCDATE of Pivot Tables in Worksheet 
  "Worksheet1" to "20190131".

ActiveSheet.PivotTables("PivotTable1").PivotFields("PROCDATE"). _

    CurrentPage = "(All)"

With ActiveSheet.PivotTables("PivotTable1").PivotFields("PROCDATE")
    .PivotItems("20190131").Visible = True

    .PivotItems("20190228").Visible = False

    .PivotItems("20190331").Visible = False

    .PivotItems("20190430").Visible = False

    .PivotItems("20190531").Visible = False

    .PivotItems("20190630").Visible = False

    .PivotItems("20190731").Visible = False

    .PivotItems("20190831").Visible = False

    .PivotItems("20190930").Visible = False

    .PivotItems("20191031").Visible = False

    .PivotItems("20191130").Visible = False

    .PivotItems("20191231").Visible = False

    ActiveSheet.PivotTables("PivotTable2").PivotFields("XDPI_PROCDATE"). _
    CurrentPage = "(All)"
    End With
End Sub

I expect that the output will be the only visible filter in the pivot table will be “20190131”. But it gets an error that the other 20190930, 20191031, 20191130, 20191231 cannot be found.

How to&Answers:

If there is only one item to select then DO NOT set each item one by one or even in a loop. Simply clear the filters and set the value to the relevant value.

See this

Manual Way

enter image description here

Via VBA Code

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim PivName As String
    Dim PivField As String
    Dim PivValue As String

    '~~> Change as applicable
    Set ws = Sheet1

    PivName = "PivotTable1"
    PivField = "Year"    '~~> PROCDATE in your case?
    PivValue = 2019      '~~> 20190131 in your case?

    With ws.PivotTables(PivName).PivotFields(PivField)
        .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:=PivValue
    End With
End Sub

Is there anyway I can put the “PROCDATE” in the “Filter” section in the PivotTable instead of “Rows”?

When you record a macro. You will get something like this

ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = "2014"

So simply change the above code to

With ws.PivotTables(PivName).PivotFields(PivField)
    .CurrentPage = PivValue
End With