Solved

Getting Defined name values while active sheet is chart tab

Posted on 2011-03-18
15
337 Views
Last Modified: 2012-05-11
I'm getting invalid results when I try to interrogate a defined name when the active sheet is a chart tab.

I was using Evaluate(definedName) to get a non-range value.

What is the proper way to retrieve value from Defined Names while the active tab is a chart sheet?

Cheers,

Dave
0
Comment
Question by:dlmille
  • 9
  • 6
15 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
Try this:

ThisWorkbook.Range("DefinedName").Value

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
The Defined name contains a text string which cannot be referenced with the Range method.  One of the msn developer help pages suggested using Evaluate.  That works from the debugger, but its not working in the macro code associated with the chart calculate event.  What happens on initialization (when I debug print the outcome), it prints the string data I have at that defined name, but then the event no longer works afterward.  I believe the Evaluate is messing it up.  I need another way to access the defined name values than Evaluate.

Dave
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
All names can be accessed with the Range method. However, I was incorrect above - the Range method is not exposed as path of the Workbook class. You have to use the Names collection:

   Evaluate(ThisWorkbook.Names.Item("DefinedName").Value)

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
No Joy - doesn't even work in the debug window.  I'm sure you tested this.  I'll create a blank workbook with this simple command and we can play with that...

see pic,

Dave
Picture1.png
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
Yes, I did test it.

What, exactly, is the value of the defined name?

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
you can see it on the print screen.  WHen I did debug.print [definedName] it displayed just below.  Its a text string...

Dave
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
Here you go.  I pared this app down to the minimum.

Its failing on the sub eventFixMyChart, in the fixMyChart module.  I've indicated in the comments where, but its where I'm trying to evaluate the value of what's stuck in the range name...  _chartwhatever_LabelRange_Settings <- Trying to get contents in a name that looks like this...

Dave
test.xlsm
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
I tried to reproduce and can't. I used 2007 with a chart sheet active.

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
Well, hopefully what I sent you will enable that.  Note the debug.print of the data only kicks off once, then after that the calculate event never pushes it to this routine again.  If you comment out the problem line, everything's peachy.  There's something about working with names while the chart sheet is active....  But there must be a way...

Dave
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
Comment Utility
I believe the problem is that the Names collection doesn't handle names with periods. The same code works with your other names such as "_Chart1_RangeLabels_Settings".

The other problem is that these are worksheet names versus workbook names so you have to go after them from the worksheet object:

   Evaluate(Sheet1.Names.Item("DefinedName").Value)

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
Periods aren't the problem, and don't come into play with this particular error (at least at this exact moment, working with the chart sheet - but don't think it was a problem with the embedded charts that they support...)  

The code is actually failing on the testRangeSettings = Evaluate(rangeLabelSettings) , but perhaps with the refinement you suggest, it will succeed.

PS -  the assignment to the range, rangeLabelName works perfectly, but of course, that's a range, not a text string...  Why the scope is not needed with this but IS needed for the string, is puzzling still.

Dave
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
What I'm trying to do is define range names that describe the chart affected, then return the Category/XValues range that's stored there.  That part works.  The enhancement I'm making is to store settings in a similar defined name (storing it works, but retrieving it is the problem) so that my chart wizardry can act on those stored settings...

My scheme was working, until this stumbling block.  I might be able to retrieve the scope of the labelrangesettings so I can build the range name via the chart's  category/XValues range so ----

anyway, I need to do that before I can test your proposed solution...

Dave
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
We're golden. I fully qualified the defined name reference and then used evaluate like this:

    Set XValuesRange = getXValuesRange(activechart)
    rangeLabelName = "_" & Replace(activechart.Name, " ", ".") & "_RangeLabels"
    rangeLabelSettings = "'" & XValuesRange.Parent.Name & "'!_" & Replace(activechart.Name, " ", ".") & "_RangeLabels_Settings"

and the Evaluate(rangeLabelSettings) worked correctly.

Any consequences to this approach versus the Evaluate(sheet.names.item(name).value) approach you suggested?

Finally, I don't need to do Evaluate anymore.  With the fully-qualified name definition:

testRangeSettings = ActiveWorkbook.Names(rangeLabelSettings).RefersTo

works properly so I don't have to use Evaluate (which can cause problems - at least it created memory problems for me after calling it a few thousand times in a loop, recently, lol)

Dave
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
If it works then you're good. I don't see any issues at this point. I still don't like the periods in the name as they do cause problems but you are not concerned.

Kevin
0
 
LVL 41

Author Comment

by:dlmille
Comment Utility
I'm indifferent and can make them underscores - will do to avoid the unanticipated.

Thanks,

Dave
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now