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]"
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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