VBA for Pivot to filter on cell value not working

shansakhi

Active Member
Joined
Apr 5, 2008
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello All,
I am trying to apply below VBA code to a pivot to filter based on cell range.
But I am getting error 400.

Sub OAL()
Dim filtvalues As Variant
Dim i As Integer, j As Integer
Dim pvt As PivotField
Dim pitm As PivotItem
filtvalues = Sheets("Pivot").Range("A1:A2")
Set pvt = Sheets("Pivot").PivotTables("PT1").PivotFields("[DDS].[Merged].[Merged]")
pvt.ClearAllFilters
For i = 1 To pvt.PivotItems.Count
Set pitm = pvt.PivotItems(i)
pitm.Visible = False
For j = 1 To UBound(filtvalues, 1) - LBound(filtvalues, 1) + 1
If pitm.Name = filtvalues(j, 1) Then
pitm.Visible = True
Exit For
End If
Next j
Next i
End Sub

I recorded the actions to see the system result of multiple selection.
How do I add below highlighted in filtvalues = Sheets("Pivot").Range("A1:A2") of above code.

Range("B7").Select
ActiveSheet.PivotTables("PT1").PivotFields("[DDS].[Merged].[Merged]"). _
VisibleItemsList = Array("[DDS].[Merged].&[BOM-LHR]")
ActiveSheet.PivotTables("PT1").PivotFields("[DDS].[Merged].[Merged]"). _
VisibleItemsList = Array("[DDS].[Merged].&[BOM-LHR]", _
"[DDS].[Merged].&[LHR-BOM]")

Range("B7").Select
End Sub



Regards,
 
A Power Pivot (OLAP Pivot) works a little differently.
See if the below works for you.
PS: I just found using pvt for a PivotField too confusing and changed it to pf

VBA Code:
Sub OAL_OLAP_Pivot_Filter()
    Dim filtvalues As Variant, pitm As Variant, aItm As Variant
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim strFltr As String, arrFltr As Variant
    Dim aItmFlts As String
  
    With Sheets("Pivot")
        Set pt = .PivotTables("PT1")
        filtvalues = .Range("A1:A2").Value
    End With

    Set pf = pt.PivotFields("[DDS].[Merged].[Merged]")
    pf.ClearAllFilters
 
    For Each pitm In pf.PivotItems
        For Each aItm In filtvalues
            aItmFlts = "[" & aItm & "]"
            If InStr(1, pitm.Name, Trim(aItmFlts), vbTextCompare) > 0 Then
            strFltr = strFltr & "|" & WorksheetFunction.Substitute(pitm.Name, "[Merged]", "&" & aItmFlts, 2)
            Exit For
            End If
        Next aItm
    Next pitm

    If strFltr <> "" Then
        arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
        pf.VisibleItemsList = arrFltr
    Else
        MsgBox "None of the values exist"
    End If
End Sub
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
A Power Pivot (OLAP Pivot) works a little differently.
See if the below works for you.
PS: I just found using pvt for a PivotField too confusing and changed it to pf

VBA Code:
Sub OAL_OLAP_Pivot_Filter()
    Dim filtvalues As Variant, pitm As Variant, aItm As Variant
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim strFltr As String, arrFltr As Variant
    Dim aItmFlts As String
 
    With Sheets("Pivot")
        Set pt = .PivotTables("PT1")
        filtvalues = .Range("A1:A2").Value
    End With

    Set pf = pt.PivotFields("[DDS].[Merged].[Merged]")
    pf.ClearAllFilters
 
    For Each pitm In pf.PivotItems
        For Each aItm In filtvalues
            aItmFlts = "[" & aItm & "]"
            If InStr(1, pitm.Name, Trim(aItmFlts), vbTextCompare) > 0 Then
            strFltr = strFltr & "|" & WorksheetFunction.Substitute(pitm.Name, "[Merged]", "&" & aItmFlts, 2)
            Exit For
            End If
        Next aItm
    Next pitm

    If strFltr <> "" Then
        arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
        pf.VisibleItemsList = arrFltr
    Else
        MsgBox "None of the values exist"
    End If
End Sub
Thank you so much it is working as desired for Merged pivot field.
But when I tried to change the Pivot field from Merged to other field name it is not working. It is showing "None of values available" even though values are available.
 
Upvote 0
Thank you so much it is working as desired for Merged pivot field.
But when I tried to change the Pivot field from Merged to other field name it is not working. It is showing "None of values available" even though values are available.
Did you change the field name reference in both places ?
Rich (BB code):
    Set pf = pt.PivotFields("[DDS].[Merged].[Merged]")
........
strFltr = strFltr & "|" & WorksheetFunction.Substitute(pitm.Name, "[Merged]", "&" & aItmFlts, 2)
 
Upvote 0
Ye
Did you change the field name reference in both places ?
Rich (BB code):
    Set pf = pt.PivotFields("[DDS].[Merged].[Merged]")
........
strFltr = strFltr & "|" & WorksheetFunction.Substitute(pitm.Name, "[Merged]", "&" & aItmFlts, 2)
Yes I did. I changed highlighted to another piovtfields.
 
Upvote 0
I have logged off for the night. Record a macro applying filters to that field.
Check the field references in the recorded macro matches your code.
Check the filter items match what you have in your list A1:A2

If that doesn’t find it show us
- the recorded macro
- your code
- your filter list (A1:A2)
 
Upvote 0
W
I have logged off for the night. Record a macro applying filters to that field.
Check the field references in the recorded macro matches your code.
Check the filter items match what you have in your list A1:A2

If that doesn’t find it show us
- the recorded macro
- your code
- your filter list (A1:A2)
I noticed that if field is in "Rows" area of pivot then it works but the macro gives a "None of the values exist" if run on "Filters" area of pivot for same field.
 
Upvote 0
W

I noticed that if field is in "Rows" area of pivot then it works but the macro gives a "None of the values exist" if run on "Filters" area of pivot for same field.
Unfortunately it works differently if you use a Page Filter. There is most likely a better way but give this a try:

VBA Code:
Sub OAL_OLAP_Pivot_Filter_PageFltr()
    Dim filtvalues As Variant, aItm As Variant
    Dim pt As PivotTable
    Dim PF As PivotField
    Dim tmpFltr As String, strFltr As String, arrFltr As Variant
    Dim aItmFlts As String
   
    With Sheets("Pivot")
        Set pt = .PivotTables("PivotTable1")
        filtvalues = .Range("A1:A2").Value
    End With

    Set PF = pt.PivotFields("[DDS].[Merged].[Merged]")
    PF.ClearAllFilters

    For Each aItm In filtvalues
        aItmFlts = "[" & aItm & "]"
        On Error Resume Next
            tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
            PF.VisibleItemsList = Array(tmpFltr)
            If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
        On Error GoTo 0
    Next aItm

    If strFltr <> "" Then
        arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
        PF.VisibleItemsList = arrFltr
    Else
        MsgBox "None of the values exist"
    End If
End Sub
 
Upvote 0
Solution
Unfortunately it works differently if you use a Page Filter. There is most likely a better way but give this a try:

VBA Code:
Sub OAL_OLAP_Pivot_Filter_PageFltr()
    Dim filtvalues As Variant, aItm As Variant
    Dim pt As PivotTable
    Dim PF As PivotField
    Dim tmpFltr As String, strFltr As String, arrFltr As Variant
    Dim aItmFlts As String
  
    With Sheets("Pivot")
        Set pt = .PivotTables("PivotTable1")
        filtvalues = .Range("A1:A2").Value
    End With

    Set PF = pt.PivotFields("[DDS].[Merged].[Merged]")
    PF.ClearAllFilters

    For Each aItm In filtvalues
        aItmFlts = "[" & aItm & "]"
        On Error Resume Next
            tmpFltr = WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
            PF.VisibleItemsList = Array(tmpFltr)
            If Err = 0 Then strFltr = strFltr & "|" & WorksheetFunction.Substitute(PF.Name, "[Merged]", "&" & aItmFlts, 2)
        On Error GoTo 0
    Next aItm

    If strFltr <> "" Then
        arrFltr = Split(Right(strFltr, Len(strFltr) - 1), "|")
        PF.VisibleItemsList = arrFltr
    Else
        MsgBox "None of the values exist"
    End If
End Sub
Apology for late reply.
The code is working as desired. Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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