Using variables with PowerPivot

tacoken

New Member
Joined
Nov 15, 2010
Messages
5
I'm trying to use VBA with PowerPivot, and I'd like to change the slicer field by using a variable. The code below was from recording a macro and works fine when the client's name is explicit as in the code below. When I try to replace Smith in the code with a string variable, I get a "Run-time error '1004': The item could not be found in the OLAP Cube." Using a variable for a filter works fine with a regular pivot table, but I must be missing something with PowerPivot. Any help would be greatly appreciated!


ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"[Holdings].[Client].[Client]").CurrentPageName = _
"[Holdings].[Client].&[Smith]"
 

Derek Brown

Well-known Member
Joined
Dec 26, 2005
Messages
2,390
Hello and welcome to The Board.
I have not yet needed to use VBA with PowerPivot but I wonder how you are changing that code.
Code:
' Original:
ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"[Holdings].[Client].[Client]").CurrentPageName = _
"[Holdings].[Client].&[Smith]"
' Possible alternative?:
Dim strCurrentPage as String
strCurrentPage = "[Holdings].[Client].&[" & myVariable & "]"
' or should that be:
strCurrentPage = "[Holdings].[Client].[" & myVariable & "]"
'
ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"[Holdings].[Client].[Client]").CurrentPageName = strCurrentPage
Note that I have not tested this.
 

tacoken

New Member
Joined
Nov 15, 2010
Messages
5
Thanks for the reply, Derek. Basically, I'm just defining a variable to use in place of Smith in the code, so that I can choose the client name elsewhere.

dim cname as string
ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"[Holdings].[Client].[Client]").CurrentPageName = _
"[Holdings].[Client].&[cname]"

I've tried different combos of quotations and ampersand (including your suggestion), and I can't get it to work. There seems to be something unique about the powerpivot that I'm missing because I've had no trouble with this with regular pivot tables.
 

Derek Brown

Well-known Member
Joined
Dec 26, 2005
Messages
2,390
Perhaps I should have added a value to 'myVariable' in the example, but I did not know where you were getting the value from.
Try it again with the modification:
Code:
' Possible alternative?:
Dim strCurrentPage as String
Dim myVariable as string
myVariable = "Smith"
strCurrentPage = "[Holdings].[Client].&[" & myVariable & "]"
' or should that be:
strCurrentPage = "[Holdings].[Client].[" & myVariable & "]"
'
ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"[Holdings].[Client].[Client]").CurrentPageName = strCurrentPage
 

tacoken

New Member
Joined
Nov 15, 2010
Messages
5
Brilliant, Derek! The first option worked perfectly. I can't thank you enough. I can't imagine how many more hours of frustration you just saved me.
 

Forum statistics

Threads
1,081,911
Messages
5,362,034
Members
400,668
Latest member
seacubs17

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...
Top