Can GETPIVOTDATA refer to the pivot table name?

G

Green Biro

Is there any way to use the pivot table name (from the table options
dialog)?

I want to make my report more maintainable and robust ie If I could say
GETPIVOTDATA("Key","Maths Exam",.....) it would be more readable than
GETPIVOTDATA("Key",Pivot!$e$4,......)

Thanks in advance for any assistance.

GB
 
D

Debra Dalgleish

You can't refer to the pivot table by name, but you could name a cell in
the pivot table, and refer to that name.

For example, name cell E4 as Math_Top and the formula would be:
GETPIVOTDATA("Key",Math_Top,.....)
 
G

Green Biro

Thanks for that.
I decided though that I would feel better using the table name and so I
created a UDF as follows:

Function PivotLocation(argName As String)
Dim pv As PivotTable
Set pv = Worksheets("Pivots").PivotTables(argName)
PivotLocation = pv.TableRange1.Address
End Function

My cell now reads:
=GETPIVOTDATA("key",INDIRECT("Pivots!"&PivotLocation("Maths")),"score",2)
and that works OK.

The reason for my posting again is that I wandered if you could take a look
at my function. I thought that it would simply be possible for my UDF to
return a reference but no matter that I set it to return a range or even a
variant, my cell calculated as #VALUE! That's why in the end I had to
return the address property (a string) and use INDIRECT to get the
reference. Here's my non working UDF. What was I doing wrong?

Function OldPivotLocation(argName As String) as Range
Dim pv As PivotTable
Set pv = Worksheets("Pivots").PivotTables(argName)
OldPivotLocation = pv.TableRange1
End Function

Cell: =GETPIVOTDATA("key",OldPivotLocation("Maths"),"score",2) returns
#VALUE!


Thanks in advance for any insight.

GB
 
D

Debra Dalgleish

Change it to:

Set OldPivotLocation = pv.TableRange1

and it should work correctly.
 
G

Green Biro

Thanks for that. I had tried it without the '.address' but I'd forgot the
all important keyword 'Set'. Normally when I make that error, I get an
'object not set' type of error. Obviously, errors get 'masked' a bit in
UDFs.
Anyway, I'm very pleased with the result so thanks again for your help.

GB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top