VBA Code - PivotTable Filter as You Type

Clybo006

New Member
Joined
Jul 20, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am so new to this that I might not even ask the question correctly. The question is stated in the title (PivotTable Filter As You Type) with a goal to convert some code that works on a table, and make it work for a PivotTable.
So far I have PivotTable named "BudgetVar" that also has a column (field?) labeled "Description".
I have created an ActiveX Text Box named "TextBox1" that is "LinkedCell" to an empty cell; Cell D7.

To test part of the code, I found I can type into the TextBox1 and it does link (type the same string) into cell D7 using this code:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False
Application.ScreenUpdating = True
End Sub


The original code (for a Table that is not a PivotTable) is:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False
ActiveSheet.ListObjects("BudgetVar").Range.AutoFilter Field:=2, Criteria1:= "*" & [D7] & "*", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub


The new similar code I found for a PivotTables is:
Sub FilterBasedOnVariable()
Dim myPivotField As PivotField
Dim filterValue As String
Set myPivotField = ActiveSheet.PivotTables("BudgetVar").PivotFields("Description")
filterValue = ActiveSheet.Range(" * " & [D7] & " * ").Value
myPivotField.CurrentPage = filterValue
End Sub


I tried to combine them into this, but I am failing horribly:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False

FilterBasedOnVariable()
Dim myPivotField As PivotField
Dim filterValue As String
Set myPivotField = ActiveSheet.PivotTables("BudgetVar").PivotFields("Description")
filterValue = " * " & [D7] & " * "
myPivotField.CurrentPage = filterValue

Application.ScreenUpdating = True
End Sub


Maybe I should be asking how to initiate the sub FilterBasedOnVariable() and get rid of sub textbox1_Change()?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I am so new to this that I might not even ask the question correctly. The question is stated in the title (PivotTable Filter As You Type) with a goal to convert some code that works on a table, and make it work for a PivotTable.
So far I have PivotTable named "BudgetVar" that also has a column (field?) labeled "Description".
I have created an ActiveX Text Box named "TextBox1" that is "LinkedCell" to an empty cell; Cell D7.

To test part of the code, I found I can type into the TextBox1 and it does link (type the same string) into cell D7 using this code:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False
Application.ScreenUpdating = True
End Sub


The original code (for a Table that is not a PivotTable) is:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False
ActiveSheet.ListObjects("BudgetVar").Range.AutoFilter Field:=2, Criteria1:= "*" & [D7] & "*", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub


The new similar code I found for a PivotTables is:
Sub FilterBasedOnVariable()
Dim myPivotField As PivotField
Dim filterValue As String
Set myPivotField = ActiveSheet.PivotTables("BudgetVar").PivotFields("Description")
filterValue = ActiveSheet.Range(" * " & [D7] & " * ").Value
myPivotField.CurrentPage = filterValue
End Sub


I tried to combine them into this, but I am failing horribly:
FilterBasedOnVariable()


Maybe I should be asking how to initiate the sub FilterBasedOnVariable() and get rid of sub textbox1_Change()?
I also tried and failed with:
Private Sub TextBox1_Change()
Application.ScreenUpdating = False
ActiveSheet.PivotTables("BudgetVar").PivotFields("Description").Range.AutoFilter Field:=2, Criteria1:= "*" & [D7] & "*", Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top