问题
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".
Sheets("Worksheet1").Select
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.
回答1:
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
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)
.ClearAllFilters
.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").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").CurrentPage = "2014"
So simply change the above code to
With ws.PivotTables(PivName).PivotFields(PivField)
.ClearAllFilters
.CurrentPage = PivValue
End With
来源:https://stackoverflow.com/questions/57815285/is-there-a-way-to-uncheck-other-items-in-a-pivot-table-except-a-specific-one