VBA Pivot Items in Power Pivot

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,749
Office Version
  1. 365
Platform
  1. Windows
What is the proper way to access the Pivot Items in Power Pivot using VBA?

When I record a macro the syntax seems to be

.CurrentPageName = "[TableName].[ColumnName].&[ItemName]"

I can get my macro to loop correctly if I get the ItemNames from another table and place that in the CurrentPageName string but I'd prefer if it could be done with either index numbers or using the "For each PivotItems in .PivotItems" loop.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">I normally use this structure to loop through item, doesn't work?

Code:
[COLOR=#101094]Dim[/COLOR][COLOR=#303336][FONT=inherit] Table [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PivotTable[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Dim[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PvI [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PivotItem[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Set[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Table [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Worksheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Analyse"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]PivotTables[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"tablename"[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]For[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Each[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PvI [/FONT][/COLOR][COLOR=#101094][FONT=inherit]In[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Table[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]PivotFields[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"fieldname"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]PivotItems
        
[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Next
[/FONT][/COLOR]
</code>
 
Last edited:
Upvote 0
Thanks for your suggestion. That works with normal pivot tables but so far I haven't figured out how to get to the PivotItems level in the Power Pivot. The cube structure seems to be totally different from the normal pivot tables.
 
Upvote 0
Hi, I'm in the same situation, trying to loop through PivotItems in a PowerPivot Pivot Table... and I cannot make it work...
Does anyone found a solution? Thanks in advance.
 
Upvote 0
So far the only solution to my problem seems to be the helper pivot table that had the pivot items from the first pt on the row field. It's not the prettiest solution in the world but it does the trick.

Since I know the table I'm using in the page field I could've used that as well. However, the dimension table I'm getting the values from has thousands of rows where as my data shouldn't have more than a hundred rows at a time I decided to stick with my quick and dirty solution.
 
Upvote 0
This sounds interesting... could you elaborate a little more? I tried with the use of a slicer, and that one also didn't work, although I still have a small hope...
 
Upvote 0
I have both PTs on Sheet1. PT1 is the one I want to filter and PT2 has the filter values (from my dimension table) in rows with the rowcount (any measure should do) in the values. The measure should filter out all the rows with no values from my pivot table and only show the ones that need to be filtered.

Here's my code:
Code:
Sub FilterPT()

Dim PT As PivotTable
Dim PTHelper As PivotTable
Dim PF As PivotField


Dim Rng As Range
Dim c As Range
Dim FirstName As String
Dim n As Integer




Application.ScreenUpdating = False




With Sheet1


    Set PT = .PivotTables(1)
   
    Set PF = PT.PageFields(1)
    
    Set PTHelper = .PivotTables(2)
    
    Set Rng = PTHelper.RowRange.Offset(1).Resize(PTHelper.RowRange.Rows.Count - 1)
        
    FirstName = PF.CurrentPageName  'Current PageFilter
    n = InStrRev(FirstName, "].")   'The last occurence of "]." in Current PageFilter (litterally the first occurence inreverse)
    FirstName = Left(FirstName, n)  'The beginning of the PageFilter before the actual filter value
        
    For Each c In Rng
        
        PF.CurrentPageName = _
        FirstName & ".&[" & c.Value & "]"
    
    Next c
        
End With


End Sub
The only catch in the code is the FirstName -part: In normal pivot tables you only need the actual filter value where as PowerPivot needs also the table & field references ( = the FirstName variable in my code).

Also note that the code will crash if you have the "Select Multiple Values" checked.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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