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

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...