How to pass a String into a Pivot Table Filter Ex:- Page Filter, Column Filter and Row Filter using VBA

all4excel

Active Member
Joined
Feb 15, 2008
Messages
435
Dear Experts,

I need to pass a string of text values separated by a comma separator to a Page Filter, Column Filter and Row Filter using VBA as I am not an expert in this type of coding.
I have a cell which contains some text in the below format
Ex: PageFilter1 = America,Africa,Bangladesh,India ( 4 Names to be shown ) in the Pivot Table

How do I get the Page Filter display only the 4 Countries shown above.

Warm Regards
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Please someone help me I am at my wits end..

Dim ShowMe As Boolean
str = InfoSheet.Cells(Count, 34).Value
selvalstr() = Split(str, ",")



PTable.ManualUpdate = True
'Set PItem = PTable.PivotFields(PIName).PivotItems

For Each PItem In PTable.PivotFields(PI).PivotItems

MsgBox PItem.Caption
For z = LBound(selvalstr) To UBound(selvalstr)

ShowMe = (PItem.Name = Trim(selvalstr(z)))
MsgBox PItem.Caption
If ShowMe Then

PItem.Visible = True
Else
'Below is the line that gets the error
PItem.Visible = False
End If

Next z
Next PItem




VBA Code:
 Dim ShowMe As Boolean
str = InfoSheet.Cells(Count, 34).Value
selvalstr() = Split(str, ",")



PTable.ManualUpdate = True
'Set PItem = PTable.PivotFields(PIName).PivotItems
    
             For Each PItem In PTable.PivotFields(PI).PivotItems
            
             MsgBox PItem.Caption
                    For z = LBound(selvalstr) To UBound(selvalstr)
                    
                        ShowMe = (PItem.Name = Trim(selvalstr(z)))
                        MsgBox PItem.Caption
                        If ShowMe Then
                            
                            PItem.Visible = True
                        Else
            'Below is the line that gets the error
                            PItem.Visible = False
                        End If

                    Next z
             Next PItem
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,137
Members
449,361
Latest member
VBquery757

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