I have been working with normal PivotTables in VBA, but I recently found some features on the Pivot Tables using the Data Model that I really like--mainly 'Distinct Count'. I have some code in a normal pivot table which filters the table for records 'Like' a string, and it works perfectly. How might I convert this code to the Pivot Table using the Data Model?
With ActiveSheet.PivotTables("Metrics").PivotFields("Reference number")
.Orientation = xlPageField
.Position = 1
.EnableMultiplePageItems = True
For i = 1 To .PivotItems.Count
If .PivotItems(i).Name Like "*oran*" Then
.PivotItems(i).Visible = False
End If
Next i
End With
Here is the code that is created when I record a macro and select the items to display manually under the Data Model:
ActiveSheet.PivotTables("Metrics").PivotFields("[RawData].[Status].[Status]"). _
VisibleItemsList = Array("[RawData].[Status].&[apple_434]", _
"[RawData].[Status].&[banana_689]", _
"[RawData].[Status].&[orange_1346]", _
"[RawData].[Status].&[orange_1454]")
This is the direction I am heading, but I am having some trouble accessing the VisibleItemsList Array:
With ActiveSheet.PivotTables("Metrics").PivotFields("[RawData].[Status].[Status]")
For i = 0 To UBound(.VisibleItemsList)
If i Like "*oran*" Then
i = ""
Debug.Print i
End If
Next i
End With
The output i is numeric 0,1,2,3,4 --not text, and the numbers do not seem to correspond to the number of items in the filter list. I can't figure out how to access the items, so I can show or hide the ones I want using code. I will be honest that I have not been working with arrays for very long.