Retrieve all items from PageFields of a pivot table

dibbler

New Member
Joined
Aug 8, 2014
Messages
2
Hi everyone,

I found the following code in a former thread and it works great for "normal" pivot tables.
http://www.mrexcel.com/forum/excel-...elp-how-retrieve-all-pivot-table-filters.html

Unfortunately my pivot tables get its data mainly from a power pivot data model.
So when I start the macro (I changed it slightly in order to write the items in a separate sheet) , all I get is the name of the page field but no items below.

This is what I get: [Pivot_MerO_DataImport].[Decided Year].[Decided Year]

'Pivot_MerO_DataImport' is the name of the table in the data model from where I get the data for the pivot table and 'Decided Yea'r is the name of the page field. Which settings do I have to carry out so that the code works also for power pivot data?

Code:
Sub Get_Current_PageFilters_OnePageField()     
Dim PT As PivotTable Dim sArray() As String Dim i As Long     
On Error Resume Next          
Set PT = Worksheets("PivotData").Range("A1").PivotTable      
If PT.PageFields.Count < 1 Then MsgBox "There are no PageFields in this Pivot Table"         
Exit Sub     
End If          
With PT.PageFields(1)        
 '---store field name in sArray(0)         
ReDim sArray(0)         
sArray(0) = .Name 
If .EnableMultiplePageItems Then        
'---store visible items in sArray(1) and up             
For i = 1 To .PivotItems.Count                 
If .PivotItems(i).Visible Then                     
ReDim Preserve sArray(UBound(sArray) + 1)                     
sArray(UBound(sArray)) = .PivotItems(i)                 
End If             
Next i         
Else             
ReDim Preserve sArray(UBound(sArray) + 1)             
sArray(UBound(sArray)) = .CurrentPage         
End If     
End With          
'---display results in Immediate Window     
For i = 0 To UBound(sArray)         
'Debug.Print sArray(i)         
Worksheets("Filter").Range("A" & i + 1).Value = sArray(i)     
Next i End Sub

And one more question: I want to start the makro every time the filter is changed and the report is updated.How do I get this solved?
(My knowledge of <acronym title="visual basic for applications">VBA</acronym> programming is rather poor)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi everybody,

I asked the same question in a german excel forum,
The following code works for power pivot. Alle selected page filter items are written into the Worksheet "Filter". The first page field is written in A1, the selected items below. The second page field is written to B1 and so on.

Code:
'Anwendung bei Pivot-Tabellenberichten mit OLAP-Cube-Datenquelle 
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)     
Dim pvField As PivotField, intV, intZeile As Integer, intSpalte As Integer, _         
pvItem As Variant, strItem As String, Zelle0 As Range     
With Me         
Set Zelle0 = Worksheets("Filter").Range("A1") 'Startzelle für das Eintragen der Werte         
If Zelle0 <> "" Then Zelle0.CurrentRegion.ClearContents         
'Alle slektierten Elemente der >Berichtsfelder in den rechten Nachbarzellen anzeigen         
intSpalte = 0         
For Each pvField In Target.PageFields             
intZeile = 0             
'Pivot-Feldname (ohne vorangestellte Elemente der Datenquelle             
strItem = pvField.Name             
strItem = Mid(strItem, InStrRev(strItem, ".[") + 2)             
strItem = Left(strItem, Len(strItem) - 1)             
Zelle0.Offset(intZeile, intSpalte).Value = strItem             
If pvField.DataRange.Text = "All" Then                 
'Alle Elemente sind gewählt                 
intZeile = intZeile + 1                 
Zelle0.Offset(intZeile, intSpalte).Value = "(Alle)"            
 ElseIf pvField.VisibleItemsList(1) = "" Then            
'Ein einzelnes Element ist selektiert - mehrfachselektion ist deaktiviert                 
intZeile = intZeile + 1                 
Zelle0.Offset(intZeile, intSpalte).Value = pvField.DataRange.Text             
Else                 
'Mehrfachselektion ist aktiviert                 
For Each pvItem In pvField.VisibleItemsList                    
'Pivot-Item (ohne vorangestellte Elemente der Datenquelle)                    
strItem = pvItem                     
strItem = Mid(strItem, InStr(1, strItem, "&") + 2)                     
strItem = Left(strItem, Len(strItem) - 1)                     
intZeile = intZeile + 1                     
Zelle0.Offset(intZeile, intSpalte).Value = strItem                 
Next             
End If             
intSpalte = intSpalte + 1         
Next     
End With 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,034
Messages
6,176,001
Members
452,695
Latest member
Alhassan

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