VBA for Power BI cube(OLAP) pivot filters

Twollaston

Board Regular
Joined
May 24, 2019
Messages
229
Hello Everyone,

I have a button that has a macro assigned to it, to load the pivot filters into 2 separate pivots based on certain cell values. It works about 75% of the time, but every so often it gives me an error message(when i have On Error Resume Next commented out) and the lines of code i get the errors on are:

Code:
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[LMA].[DealerId].[DealerId]").VisibleItemsList = Array("")

ActiveSheet.PivotTables("CanceledDealer").PivotFields("[Time].[Time YM].[Year]").VisibleItemsList = Array("")

ActiveSheet.PivotTables("LMA").PivotFields("[Time].[Time YM].[Year]").VisibleItemsList = Array("")
I'm not sure why I'm receiving errors for these only sometimes and other times it's working fine. I was wondering if anyone has had similar issues and has an idea why this might occur? Before doing it this way, I was doing it differently using something like with pt and then setting the pivot fields = to the desired filter, but it was also having a lot of errors so I tried to use the visible items list method. One thing I did notice is that when I right click the report filters and click field settings, sometimes it says the Name is "Year" or "DealerId", but then other times it is referencing them like this [LMA].[DealerId].[DealerId] so I'm also not sure why this is changing and not showing the same thing every time I look at it.

I have this for a report that will be used a lot within the company and I'm trying to get it so that it never errors out so any advice would be greatly appreciated.

Here is the full code:


Code:
Sub PivotFilters()




'Want to Continue Message?
If MsgBox("It will take approximately 5 minutes for this data to load. Are you sure you want to continue?", vbYesNo) = vbNo Then Exit Sub




'Declaring Variables
Dim NewFilter As String
Dim NewFilter2 As String




'Assigning Values to Variables
NewFilter = Worksheets("Market_Data").Range("R2").Value
NewFilter2 = Worksheets("Market_Data").Range("R3").Value




'Reduce lag, not show page switches while query runs
Application.ScreenUpdating = False




'Deactivate Error Macro Interruption
On Error Resume Next




'Go To Market Data Sheet
Sheets("Market_Data").Select




'Clear CanceledDealer Pivot Filter for Dealer ID and filter for value in R3 on market data spreadsheet
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[LMA].[DealerId].[DealerId]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[LMA].[DealerId].[DealerId]").VisibleItemsList = Array("[LMA].[DealerId].&[" & NewFilter2 & "]")




'Clear LMA Pivot Filter for Time, and add in the value from R2 on the market data spreadsheet
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[Time].[Time YM].[Year]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[Time].[Time YM].[Month]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("CanceledDealer").PivotFields("[Time].[Time YM].[Month]").VisibleItemsList = Array("[Time].[Time YM].[Month].&[" & NewFilter & "]")
        
        
'Clear LMA Pivot Filter for Time, and add in the value from R2 on the market data spreadsheet
ActiveSheet.PivotTables("LMA").PivotFields("[Time].[Time YM].[Year]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("LMA").PivotFields("[Time].[Time YM].[Month]").VisibleItemsList = Array("")
ActiveSheet.PivotTables("LMA").PivotFields("[Time].[Time YM].[Month]").VisibleItemsList = Array("[Time].[Time YM].[Month].&[" & NewFilter & "]")
     
     
'Go to Market Chart page
Sheets("Page5_New").Select
     
     
'Reduce lag, not show page switches while query runs
Application.ScreenUpdating = True




'Reactivate Error Macro Interruption
On Error GoTo 0


End Sub
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Watch MrExcel Video

Forum statistics

Threads
1,090,140
Messages
5,412,691
Members
403,441
Latest member
speedtobeat

This Week's Hot Topics

Top