Está en la página 1de 6

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

You are here: Home Computing/Technology Business Software Excel Customizing Charts' Series Names w/ Excel VBA

Excel/Customizing Charts' Series Names w/ Excel VBA


Advertisement

Expert: Bill - 10/1/2008

Question QUESTION: Hi Bill, Over the past week I have been learning VBA in order to write a macro which generates one chart for each person in a sheet, where there may be any number of people and observations per person. I managed to get it working with one exception. I can not figure out how to make Excel use the correct label for each person's series (it should be their ID number, which is in column B of the same row as their source data). I used a named range, working with a relative selection and find command, to identify each person's source data. Their ID is two cells left of the beginning of their range. To my knowledge the 'ActiveChart.SeriesCollection(1).Name = ' command does not allow any flexibility like the

1 of 6

27/01/2013 2:35 PM

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

'ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("Currentselection"), PlotBy:= _ xlRows' command did. Any thoughts? I will greatly appreciate any help. There are 14 modules and they are long, so below I am only pasting the most relevant code. Thanks much, Jason Miller Code: Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("Currentselection"), PlotBy:= _ xlRows ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).XValues = "=Sheet1!R1C4:R1C20" ActiveChart.SeriesCollection(1).Name = [This is where I need help!!] ActiveChart.SeriesCollection(2).Values = "=Sheet2!R1C2:R1C18" ActiveChart.SeriesCollection(2).Name = "=Sheet2!R1C1" ActiveChart.Location Where:=xlLocationAsNewSheet

ANSWER: Try just creating a variable and getting that name first and then use the variable in the code. Example (UNTESTED) SeriesName as String SeriesName = Sheets("Sheet1").Range("C1").Offset(0,-2) ActiveChart.SeriesCollection(1).Name = SeriesName Change the range reference as needed HTH Bill

---------- FOLLOW-UP ---------QUESTION: Bill, Thanks very much for your quick help, I would not have thought to try that. I tested it (and a fwe variations) but it returns the following run-time error:

2 of 6

27/01/2013 2:35 PM

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

"Run time error '1004' Unable to set Name property of the Series class". The help file associated with that error is very broad, thus not helpful. If you have any other ideas that would be great; otherwise I appreciate your help regardless. Jason ANSWER: I don't write code for charts often but the Name of the series should accept any string. Try just adding ActiveChart.SeriesCollection(1).Name = "Jason" to see if that works. If it does then SeriesName as String SeriesName = Sheets("Sheet1").Range("C1").Offset(0,-2) ActiveChart.SeriesCollection(1).Name = SeriesName should work. IF that does not work then try just recording a macro and plug any name for a series. Look at that code to see what EXCEL is expecting. Make sure the Sheets("Sheet1").Range("C1").Offset(0,-2) has something in it too. ---------- FOLLOW-UP ---------QUESTION: Hi Bill Thanks again for your help. I know this is rather obscure (a great first-VBA-project, eh?). You are right that it can accept a string like ="Jason". At first it your example gave an error about not being in a type block, but that went away when I added Dim. So that would be great, but the catch is that in this example we are still using an absolute (non-changing) cell reference (C1). So I need it to use the named range (or something else?) to select the correct cell for each chart. Given this I try: "Dim SeriesName As String SeriesName = Sheets("Sheet1").Range("CurrentSelection").Offset(0, -2)" Where "CurrentSelection" is the named range which is different for each person (subtotal group) and which works for identifying the series' actual value (source data). But in this context it sticks me with an error saying "Run time error 13: Type Mismatch"....any ideas?

3 of 6

27/01/2013 2:35 PM

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

Thanks again, Jason Answer 0 Yes. When you declare a variable, and using DIM is one way of doing so, it is often a good idea to tell VBA what type of data you intend to pass that variable so that VBA does not have to work so hard to figure that out (Dim X as String vs. just Dim X). This is referred to as "typing" your variables - what "type" of data. So, in this case the type mismatch says that you are trying to put a different type of data into the variable than it is expection and will accept. So, the contents of Sheets("Sheet1").Range("CurrentSelection").Offset(0, -2) is a not a string. In this case, the chart series name may need a string and the data in the cell may not or ever be a string. I don't know since I have not seen the data or the workbook. So, you can convert the data in the cell to a string using the CStr function. Example: SeriesName = CStr(Sheets("Sheet1").Range("CurrentSelection").Offset(0, -2)) Maybe this will help

Questioner's Rating Rating(1-10) Knowledgeability = 8 Comment

Clarity of Response = 10

Politeness = 10

Bill, Thanks much for your help. The data actually is a string (it's "Panelist 1", or "Panelist 2", etc). But I tried the code (coverter) and it still gives me that type mismatch error. I feel bad about abusing your kindness with repeated questions however, so I wanted to go ahead and official thank/rate you on here. I'll keep wrestling with the VBA; thanks again Jason

Add to this Answer Ask a Question


Related Articles

Range Name - Excel Range Name - Using a Range Name in Excel Formulas Excel 2010 Tutorial - Excel 2010 Step by Step Tutorial - Excel 2010 Spreadsheet Tutorial Accessing and managing MS Excel sheets with Delphi (Page 3/7) Renaming Your Image Files - Digital Photography Radio Frequency Interference in RC Toys - Choosing Your RC Car or Truck Frequency for RC Toys
About Excel This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

4 of 6

27/01/2013 2:35 PM

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

Excel

All Answers Answers by Expert: Tom Ogilvy Bob Umlas Aidan Heritage Bill Bill Hermanson Richard Roberts Stuart Resnick Richard Stephens Jan Karel Pieterse Adelaide carvalho Nathan Head Jerry Beaucaire Richard Rost Sixthsense Damon Ostrander Tushar Sakhalkar Edward H. Mori Anne Troy Gyula Gulyas Jessica L. Victor Lan Shahid Chad Welch Ariful Mondal David W. Cofer Mary A. Lindsey Philip Buckley GULSHAN PURSWANI Amal Garreth Dave Morrison Michelle Howell Ask Experts Volunteer

Bill
Expertise

5 of 6

27/01/2013 2:35 PM

Excel: Customizing Charts' Series Names w/ Excel VBA, excel vba, seri...

http://en.allexperts.com/q/Excel-1059/2008/10/Customizing-Charts-Seri...

I can provide help with most all EXCEL questions and most all questions about writing EXCEL macros. I have been developing macros for about 10 years in EXCEL and have switched to it from Lotus 1-2-3 after about 10 years of writing macros in it. Typically, I will not write a macro for you unless it is very short because of all the details a macro has to know about to work every time all the time. Please understand that I do not know it all and will be the first to say so. As politely as possible, I don't write macros for people on this site who need one, want one, seem to imply that they need one, and/or seem to think I am expected to write one UNLESS they are very short, quick, and simple. 99% of all macros are more involved than what you think and rarely am I provided with enough specific and complete details to have the code work the first time and every time. This typically means too many follow-up emails, and subsequent macro changes due to lack of specific details, just to get those details so that the macro would work, all of which is on my own free time. The voice of experience from responding to many questions from people who ask me to write a macro for them from this site tells me this. I don't mean to come across as unhelpful but macros are usually very specific and without ALL of the specifics the macro I would write will not address all of your needs and the layout, location, formatting, conditions, etc. of your data and any related files the macro would have to work with. What seems like a simple task to you is almost always more involved than what you think to have the macro ALWAYS work in EVERY situation. If you have a macro you have already written and have a question about it then perhaps I could help with that. I am sure and hope you can and do understand. User Agreement Privacy Policy 2013 About.com. All rights reserved. About Us Tell Friends How to Ask How to Volunteer FAQ Tech Support Top Experts Expert Login Browse Answers: By Category Alphabetically Find expert answers to:

6 of 6

27/01/2013 2:35 PM

También podría gustarte