VBA Pivot Table make visible values from a field based on a range

k0023382

New Member
Joined
Feb 25, 2010
Messages
45
A very simple thing, I do not get my head around.

On a given cell, I could have A1= "1:3:7", it means 3 ID locations
Ideally I would to transform this string onto an array in VBA.

Array(0)="1"
Array(1)="3"
Array(2)="5"

The ":" is the character that separate each element. Sometimes the string has only 1 item no more separator, others it is long with more than 20 items on it. Items could be text or numbers.
And finally apply those to a field on a Pivot table called Location

Below the code

VBA Code:
Public Sub PV_MutipleItems2()

Dim arr() As String

' Extract his value from the cell for Location
Dim strP As String: strP = "1:3:5" 'test, originally from Range("A1").value
Dim strSpecialC As String: strSpecialC = ":"

arr() = Split(strP, strSpecialC)

If UBound(arr) = 0 Then
        'make visible only 1 item
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Location")
            .ClearAllFilters
            .PivotItems.Name.Visible = arr(0)
        End With
    Else
        'make visible more than 1 item
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Location") '.PivotFilters
            .ClearAllFilters
            For Each Pi In .PivotItems
                For i = LBound(arr) To UBound(arr)
                    If Pi.Name = arr(i) Then
                            Pi.Visible = True
                        Else
                            Pi.Visible = False
                    End If
                Next i
            Next
        End With
End If

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

k0023382

New Member
Joined
Feb 25, 2010
Messages
45
A little mistake,

instead of
VBA Code:
       'make visible only 1 item
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Location")
            .ClearAllFilters
            .PivotItems.Name.Visible = arr(0)
        End With

it shoud be
VBA Code:
'make visible only 1 item
ActiveSheet.PivotTables("PivotTable2").PivotFields("Location").CurrentPage = arr(0)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,021
Messages
5,639,583
Members
417,100
Latest member
Simon123456789

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
Top