I'm trying to set up a macro to filter a pivot table based on a user defined range.
So far all I have is the prompt to get the range. Unfortunately, I have no idea how to take the values from the range and apply it to the filter. Thanks for your help!
Sub foreachloop()
Dim wbmodel As Excel.Workbook
Set wbmodel = ActiveWorkbook
Dim rng As Range
Dim Filter As String
With wbmodel.Sheets("Pivot_model").PivotTables("Modelpivot")
.ClearAllFilters
Set rng = Application.InputBox("select Range", "Inputs", Type:=8)
Filter = Application.InputBox("select Filter", "Inputs", Type:=8)
MsgBox rng.Address
MsgBox Filter
For Each PivotItem In .PivotFields(Filter).PivotItems
Select Case PivotItem.Name
Case rng
PivotItem.Visible = True
Case Else
PivotItem.Visible = False
End Select
Next PivotItem
End With
End Sub
Ok. so I made some (slight) updates. I made my first "For Each" loop. Now, I know I will need to do a similar "for each" loop to apply the filter. I've been battling with the code for a few hours and I am unsure where to go next.