VBA for Power BI cube(OLAP) pivot filters

Twollaston

Board Regular
Joined
May 24, 2019
Messages
241
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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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