VBA for Power BI cube(OLAP) pivot filters


Board Regular
May 24, 2019
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:

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:

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

'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
'Reduce lag, not show page switches while query runs
Application.ScreenUpdating = True

'Reactivate Error Macro Interruption
On Error GoTo 0

End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...