Passing a variable to a pivot filter.

tbablue

Active Member
Joined
Apr 29, 2007
Messages
450
Hi Forum,

Some help req'd with passing a variable to a pivot filter.

How would I amend the following code such that PivotItems("99999") could be replaced by the value from a cell - a named range, [CandID_Picker]? The Pivot Table needs to filter on CandID_Picker - and nothing else.

With Activesheet.PivotTables("CandID_Picker_table").Pivotfields("CandID")
.ClearAllFilters
.PivotItems("99999").Visible = True
End With

Any help appreciated.
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,174
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Try just:

Code:
.PivotItems("" & [CandID_Picker]).Visible = True
although if you've just cleared all filters, I don't know why it wouldn't already be visible.
 
Last edited:

tbablue

Active Member
Joined
Apr 29, 2007
Messages
450
TY for your attention Rory. Most grateful.

I want to make all other PivotItems.Visible = False
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,209
Office Version
2007
Platform
Windows
Try this

If the data to be filtered does not exist in the pivot table, an error is generated, because you must select at least one data.
With the following check if the data does not exist, then sends the message "No match".

Or maybe you can verify the data in the original database, before filtering.

Code:
Sub Filter_Pivot()
  Dim pi As PivotItem, n As Long
  
  Application.ScreenUpdating = False
  n = 0
  With ActiveSheet.PivotTables("CandID_Picker_table").PivotFields("CandID")
    .ClearAllFilters
    For Each pi In .PivotItems
      If LCase(pi) <> LCase([CandID_Picker]) Then
        n = n + 1
        If n < .PivotItems.Count Then
          pi.Visible = False
        Else
          MsgBox "No match"
          .ClearAllFilters
        End If
      End If
    Next
  End With
End Sub
 

tbablue

Active Member
Joined
Apr 29, 2007
Messages
450
Hi Dante,

Really grateful for your attention.

The code fails at pi.Visible = False - I can't immediately see why.

Any idea?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,209
Office Version
2007
Platform
Windows
I made the comment at the same time I answered, we must first look for the data.

Try this

Code:
Sub Filter_Pivot()
  Dim pi As PivotItem, f As Range
  Application.ScreenUpdating = False
  With ActiveSheet.PivotTables("CandID_Picker_table").PivotFields("CandID")
    .ClearAllFilters
    Set f = Range(.DataRange.Address).Find([CandID_Picker], , xlValues, xlWhole)
    If f Is Nothing Then
      MsgBox "No match"
    Else
        For Each pi In .PivotItems
          If LCase(pi) <> LCase([CandID_Picker]) Then pi.Visible = False
        Next
    End If
  End With
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,209
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,731
Messages
5,488,543
Members
407,645
Latest member
suyoggore

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top