I have a pivot table and I've created a UserForm which should allow the user to select certain filtering options. I would like to filter the rows based on various criteria linked to columns, depending on user input. However, I can't seem to get it to filter the rows on multiple value filters. This doesn't seem to be possible from the front end either, but I'm still hoping that VBA will find a way.
This is how the relevant code (behind the UserForm's OK button) starts off:
'ok button - this is the big one, telling the button what to do with the selected options!
Private Sub CommandButton1_Click()
'targeting our familiar pivot table
Dim pvt As PivotTable
Set pvt = Sheets("Summary").PivotTables("PivotTable1")
'allowing multiple filters to be used at once
pvt.AllowMultipleFilters = True
'and identifying the row that we want to filter - n.b. this would need to change if corporate wanted to use another field as their lowest level row.
Dim pvtFieldBrand As PivotField
Set pvtFieldBrand = pvt.PivotFields("final brand")
'clearing any filters already set
pvtFieldBrand.ClearAllFilters
Then come the filters.
This one works in isolation:
'Filtering depending on whether 1+, 2+ or all 3 audiences favour it
Dim pvtFieldNoAud As PivotField 'then hide the column - at the top of this script, in fact, just in case it's not already hidden
Set pvtFieldNoAud = pvt.PivotFields("Sum of # Audiences")
If OptionButton3 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsBetween, DataField:=pvtFieldNoAud, Value1:=1, Value2:=3
End If
If OptionButton4 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsBetween, DataField:=pvtFieldNoAud, Value1:=2, Value2:=3
End If
If OptionButton5 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueEquals, DataField:=pvtFieldNoAud, Value1:=3
End If
Then come these filters, which I intended to be multi-select options, but which will only work in complete isolation - not in combination with each other, and not in combination with any other filters:
'Only show if favoured by the selected audiences
Dim pvtFieldWW As PivotField
Set pvtFieldWW = pvt.PivotFields("Sum of WW overindex?")
Dim pvtFieldUU As PivotField
Set pvtFieldUU = pvt.PivotFields("Sum of UU overindex?")
Dim pvtFieldRR As PivotField
Set pvtFieldRR = pvt.PivotFields("Sum of RR overindex?")
If CheckBox1 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldWW, Value1:=0
End If
If CheckBox2 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldUU, Value1:=0
End If
If CheckBox3 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldRR, Value1:=0
End If
And so on.
I need users to be able to filter this pivot table in up to 6 ways. It could probably be done with Slicers and Report Filters from the front end, but I want to restrict the user's options and the potential for user error, so I was planning on keeping all the relevant options in the UserForm and then locking some functionality so they can't make any other changes to the pivot table. Any ideas much appreciated!