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

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...