B
Bryan
I am trying to execute a command where a user can input the criteria that
they want and have the pivot table show the specified item in the field they
want filtered. I am using the code below, however, it does not completely
erase the last item that was selected in the field value. It will always keep
two items displayed.
IS there a problem in the code below? Also, how can I make this selection
apply to more than one pivot table, would like this selection to feed 4
different tables.
Sub Selection()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim strPromptPF As String
Dim strPromptPI As String
Dim strPF As String
Dim strPI As String
strPromptPF = "Please enter the name of the field you wish to filter."
strPromptPI = "Please enter the item you wish to filter for."
Set pt = ActiveSheet.PivotTables("PivotTable1")
strPF = InputBox(strPromptPF, "Enter Field Name")
strPI = InputBox(strPromptPI, "Enter Item")
Set pf = pt.PivotFields(strPF)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
With pf
.AutoSort xlManual, .SourceName
For Each pi In pf.PivotItems
pi.Visible = False
Next pi
.PivotItems(strPI).Visible = True
.AutoSort xlAscending, .SourceName
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
'
End Sub
they want and have the pivot table show the specified item in the field they
want filtered. I am using the code below, however, it does not completely
erase the last item that was selected in the field value. It will always keep
two items displayed.
IS there a problem in the code below? Also, how can I make this selection
apply to more than one pivot table, would like this selection to feed 4
different tables.
Sub Selection()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim strPromptPF As String
Dim strPromptPI As String
Dim strPF As String
Dim strPI As String
strPromptPF = "Please enter the name of the field you wish to filter."
strPromptPI = "Please enter the item you wish to filter for."
Set pt = ActiveSheet.PivotTables("PivotTable1")
strPF = InputBox(strPromptPF, "Enter Field Name")
strPI = InputBox(strPromptPI, "Enter Item")
Set pf = pt.PivotFields(strPF)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
With pf
.AutoSort xlManual, .SourceName
For Each pi In pf.PivotItems
pi.Visible = False
Next pi
.PivotItems(strPI).Visible = True
.AutoSort xlAscending, .SourceName
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
'
End Sub