VBA for Pivot to filter on cell value not working

shansakhi

Active Member
Joined
Apr 5, 2008
Messages
276
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,
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this:

VBA Code:
Sub OAL()
  Dim filtvalues As Variant, pitm As Variant, aItm As Variant
  Dim pt As PivotTable
  Dim pvt As PivotField
  Dim bExists As Boolean
  Dim n As Long
  
  With Sheets("Pivot")
    Set pt = .PivotTables("PT1")
    filtvalues = .Range("A1:A2").Value
  End With
  
  Set pvt = pt.PivotFields("DDS")
  pvt.ClearAllFilters
  
  For Each pitm In pvt.PivotItems
    bExists = False
    For Each aItm In filtvalues
      If LCase(Trim(aItm)) = LCase(pitm.Value) Then
        bExists = True
        Exit For
      End If
    Next
    If bExists = False Then
      n = n + 1
      If n < pvt.PivotItems.Count Then
        pitm.Visible = False
      Else
        pvt.ClearAllFilters
        MsgBox "None of the values exist"
     End If
    End If
  Next
End Sub

Note Code Tag:
In future please use code tags when posting code.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Thank you for the reply. But same error of 400
I have loaded the data in table called DDS and then creating a pivot table based on this.


Sub OAL()
Dim filtvalues As Variant, pitm As Variant, aItm As Variant
Dim pt As PivotTable
Dim pvt As PivotField
Dim bExists As Boolean
Dim n As Long

With Sheets("Pivot")
Set pt = .PivotTables("PT1")
filtvalues = .Range("A1:A2").Value
End With

Set pvt = pt.PivotFields("DDS") 'DDS is Table name and merged is Pivotfield name
pvt.ClearAllFilters

For Each pitm In pvt.PivotItems
bExists = False
For Each aItm In filtvalues
If LCase(Trim(aItm)) = LCase(pitm.Value) Then
bExists = True
Exit For
End If
Next
If bExists = False Then
n = n + 1
If n < pvt.PivotItems.Count Then
pitm.Visible = False
Else
pvt.ClearAllFilters
MsgBox "None of the values exist"
End If
End If
Next
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub OAL()
  Dim filtvalues As Variant, pitm As Variant, aItm As Variant
  Dim pt As PivotTable
  Dim pvt As PivotField
  Dim bExists As Boolean
  Dim n As Long
 
  With Sheets("Pivot")
    Set pt = .PivotTables("PT1")
    filtvalues = .Range("A1:A2").Value
  End With
 
  Set pvt = pt.PivotFields("DDS")
  pvt.ClearAllFilters
 
  For Each pitm In pvt.PivotItems
    bExists = False
    For Each aItm In filtvalues
      If LCase(Trim(aItm)) = LCase(pitm.Value) Then
        bExists = True
        Exit For
      End If
    Next
    If bExists = False Then
      n = n + 1
      If n < pvt.PivotItems.Count Then
        pitm.Visible = False
      Else
        pvt.ClearAllFilters
        MsgBox "None of the values exist"
     End If
    End If
  Next
End Sub

Note Code Tag:
In future please use code tags when posting code.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
I tried changing the
pt.PivotFields("DDS") to pt.PivotFields("[DDS].[merged].[merged]")
It clear the filters but do not take the filvalues range A1:A2


Sub OAL1()
Dim filtvalues As Variant, pitm As Variant, aItm As Variant
Dim pt As PivotTable
Dim pvt As PivotField
Dim bExists As Boolean
Dim n As Long

With Sheets("Pivot")
Set pt = .PivotTables("PT1")
filtvalues = .Range("A1:A2").Value
End With

Set pvt = pt.PivotFields("[DDS].[merged].[merged]")
pvt.ClearAllFilters

For Each pitm In pvt.PivotItems
bExists = False
For Each aItm In filtvalues
If LCase(Trim(aItm)) = LCase(pitm.Value) Then
bExists = True
Exit For
End If
Next
If bExists = False Then
n = n + 1
If n < pvt.PivotItems.Count Then
pitm.Visible = False
Else
pvt.ClearAllFilters
MsgBox "None of the values exist"
End If
End If
Next
End Sub
 
Upvote 0
VBA Code:
Sub OAL1()
  Dim filtvalues As Variant, pitm As Variant, aItm As Variant
  Dim pt As PivotTable
  Dim pvt As PivotField
  Dim bExists As Boolean
  Dim n As Long
 
  With Sheets("Pivot")
    Set pt = .PivotTables("PT1")
    filtvalues = .Range("A1:A2").Value
  End With
 
  Set pvt = pt.PivotFields("[DDS].[merged].[merged]")
  pvt.ClearAllFilters
 
  For Each pitm In pvt.PivotItems
    bExists = False
    For Each aItm In filtvalues
      If LCase(Trim(aItm)) = LCase(pitm.Value) Then
        bExists = True
        Exit For
      End If
    Next
    If bExists = False Then
      n = n + 1
      If n < pvt.PivotItems.Count Then
        pitm.Visible = False
      Else
        pvt.ClearAllFilters
        MsgBox "None of the values exist"
     End If
    End If
  Next
End Sub
 
Last edited by a moderator:
Upvote 0
I am trying to add code as suggested by DanteAmor, but unable to do so.
AM I doing something different.
 
Upvote 0
AM I doing something different.
Yes, you put both the code tags after the code. Read the instructions carefully again. You should end up with one tag before the code and one after, like this.
[CODE=vba]
Your code goes here
[/CODE]

I have fixed it for you this time.
 
Upvote 0
THa
Yes, you put both the code tags after the code. Read the instructions carefully again. You should end up with one tag before the code and one after, like this.
[CODE=vba]
Your code goes here
[/CODE]

I have fixed it for you this time.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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