Re: OLAP Cube Pivot Table - Get Current Page Items Selected
Thanks for all the help : D , I got it working eventually. I used the "Worksheet_SelectionChange" function to detect when the pivottable is selected to run the update. rather than the "Worksheet_PivotTableUpdate" function because when it runs it causes the code to loop.
It may not be the best but it's working, and if you have any suggestions for improvement I'd be more than happy to use them .
Here's the code in the worksheet:
Code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim PT As PivotTable
cell_column = ActiveCell.Column
cell_row = ActiveCell.Row
cell_id = ColumnLetter(cell_column) & cell_row
On Error GoTo ErrorHndl
Set PT = Range(cell_id).PivotTable
If PT <> "" Then
Call SyncOLAPBasedPivotTablesWithMultiDimsAndMultiSelect(PT)
End If
ErrorHndl:
End Sub
Function ColumnLetter(ByVal colNum As Long) As String
Dim s As Long, x As Long
For s = Int(Log(CDbl(25 * (CDbl(colNum) + 1))) / Log(26)) - 1 To 0 Step -1
x = (26 ^ (s + 1) - 1) / 25 - 1
If colNum > x Then
ColumnLetter = ColumnLetter & Chr(((colNum - x - 1) \ 26 ^ s) Mod 26 + 65)
End If
Next s
End Function
Display More
And here's the code I modified from your suggestion:
Code
Function SyncOLAPBasedPivotTablesWithMultiDimsAndMultiSelect(active_pivottable)
Dim PT As PivotTable
Dim PT2 As PivotTable
Dim PF As PivotField
Dim PF2 As PivotField
Dim CPICounter As Integer
Dim PF_Check As PivotField
Set PT = ActiveSheet.PivotTables("PivotTable1")
Set PT2 = ActiveSheet.PivotTables("PivotTable2")
Set PF = PT.PivotFields("[Call Date]")
Set PF2 = PT2.PivotFields("[Logged Date]")
PF.CubeField.EnableMultiplePageItems = True
PF2.CubeField.EnableMultiplePageItems = True
If active_pivottable = "PivotTable1" Then
Set PF_Check = ActiveSheet.PivotTables("PivotTable1").PivotFields("[Call Date]")
For CPICounter = 1 To PageCount(PF_Check)
If CPICounter = 1 Then
PF2.AddPageItem "[Logged Date].[All Logged Date]" & Mid(PF.CurrentPageList(CPICounter), 28), True
Else
PF2.AddPageItem "[Logged Date].[All Logged Date]" & Mid(PF.CurrentPageList(CPICounter), 28), False
End If
Next CPICounter
ElseIf active_pivottable = "PivotTable2" Then
Set PF_Check = ActiveSheet.PivotTables("PivotTable2").PivotFields("[Logged Date]")
For CPICounter = 1 To PageCount(PF_Check)
If CPICounter = 1 Then
PF.AddPageItem "[Call Date].[All Call Date]" & Mid(PF2.CurrentPageList(CPICounter), 32), True
Else
PF.AddPageItem "[Call Date].[All Call Date]" & Mid(PF2.CurrentPageList(CPICounter), 32), False
End If
Next CPICounter
End If
End Sub
Function PageCount(PF_Check As PivotField) As Integer
On Error GoTo ErrOut
For PageCount = 1 To 200
Debug.Print PF_Check.CurrentPageList(PageCount)
Next PageCount
ErrOut:
PageCount = PageCount - 1
End Function
Display More