Show Specific Item in Specific Field

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top