Is it possible to filter a Pivot Table field using a "contains" keyword from another cell? - VBA Question

andya

New Member
Joined
May 10, 2009
Messages
2
Hi Everyone,

Is it possible to filter a PivotTable field using a "contains" keyword from another cell?

I tried recording a macro and got results below:


-----------------
Sub CatFilter()
'
' CatFilter Macro
'

'

ActiveSheet.PivotTables("PivotTable6").PivotFields("Category"). _
ClearLabelFilters
ActiveSheet.PivotTables("PivotTable6").PivotFields("Category").PivotFilters. _
Add Type:=xlCaptionContains, Value1:="Green"
End Sub
------------


I'd like to replace Value1:="Green" to reference by cell G25 where the keyword is listed. Below is a screenshot of what I attempting to do.

Thank you in advance for your help!!!

Excel01.JPG
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

You could write some code to loop through the pivot field items - best with two loops. First to make every item visible and check if there will be any to show (to avoid the error of trying to hide every single item) and the second loop to hide the items which should be hidden. Suggest setting the pivot cache to manual update at the start of the code and reset it at the end. There will be examples in some old threads - I'll have a look today.

Another way which may not be ok for you, but would be much less work is to use a parameterised query. Such as http://www.dailydoseofexcel.com/archives/2005/10/28/show-all-with-a-range-parameter-query/ The parameterised query can be set to refresh automatically on change to the input cell - your cell showing "Green". If you really need a pivot table, give the query results a defined name and make that the source data for the pivot table. The query could be on a hidden sheet. The code then is just a single line to refresh the pivot table. wksCodeRef.pivottables(1).pivotcache.refresh

HTH, Fazza
 
Upvote 0
Thank you Fazza!

I will the the above suggestions a try. The reason why I wanted to have the Pivot Table reference cell "G25" was to make it easier for people who were not comfortable with Excel.
 
Upvote 0
OK. You can still use cell G25 - just make it the cell that controls the parameterised query.

I thought overnight of a better approach than the query table. Have a worksheet change event edit the pivot table's SQL on changes to cell G25.

Such as, create the pivot table in a new workbook (separate to the data workbook, to avoid memory leak problems). Once created, you can move the resultant worksheet back into the source data file. When creating the pivot table, take the external data option at the first page of the wizard. At the completed pivot tables, insert the code below - modify to suit - into the worksheet's code module.

Regards, Fazza

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$G$25" Then
    Application.EnableEvents = False
 
    Me.PivotTables(1).PivotCache.Sql = Join$(Array( _
        "SELECT *", _
        "FROM YourSourceDataReference", _
        "WHERE Colors Like '%' & '" & Target.Value2 & "' & '%'"), vbCr)
 
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
My quick & dirty solution was a simple "find & replace" exercise using wildcards.

For example, I had shee of location data. Street names in one column, zip codes in another, location type in another.

I wanted to filter on any location that was a "school".

Since the "location type" values didn't have a uniform naming convention, I had to find all records that simply has "school" somewhere in the "location type" column.

To do this, I first made a copy of the sheet to a new tab, then on the new tab, highlighted the "location type" column, and did a "Find & Replace".

My "Find What" value was "*school* (remove quotes).

My "Replace with" value was "school"

I then sorted the records by the "location type" value, grouping all "school" locations together.

OK. You can still use cell G25 - just make it the cell that controls the parameterised query.

I thought overnight of a better approach than the query table. Have a worksheet change event edit the pivot table's SQL on changes to cell G25.

Such as, create the pivot table in a new workbook (separate to the data workbook, to avoid memory leak problems). Once created, you can move the resultant worksheet back into the source data file. When creating the pivot table, take the external data option at the first page of the wizard. At the completed pivot tables, insert the code below - modify to suit - into the worksheet's code module.

Regards, Fazza

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$G$25" Then
    Application.EnableEvents = False
 
    Me.PivotTables(1).PivotCache.Sql = Join$(Array( _
        "SELECT *", _
        "FROM YourSourceDataReference", _
        "WHERE Colors Like '%' & '" & Target.Value2 & "' & '%'"), vbCr)
 
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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