Using a variable for worksheet name in SERIES

R

reynoldscm

Hi,
I am trying to reference a dynamic range in my code that modifies a
existing chart SERIES statement. However, whenever I enter the dynami
range name (i.e. rngACWP) Excel inserts the workbook file name i
addition. I have tried referencing the sheet name...which didn't work
and then tried to establish a variable that referenced th
ActiveWorkbook name but don't think I did right. So, I basically hav
two questions:
1. Is there a way to utilize some sort of variable in the SERIE
statement so that the user isn't limited to a filename that I creat
for them but dynamically pulls their filename to use in the formula?
2. If the above anwer is yes, how would I define the variable an
utilize it in the SERIES statement?

Here is the snippet of code I am working with:
ActiveSheet.ChartObjects("CUM Chart").Activate
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(3).Select
ActiveChart.SeriesCollection(3).Formula = _
"=SERIES('RangeData'!R1C4,'Automated CSPR Charts -down to
buttons.xls'!'rngDate','Automated CSPR Charts -down to
buttons.xls'!'rngACWP',3)"

Thanks for the help.
Chri


-
reynoldsc
 
J

Jon Peltier

Chris -

As usual, Excel provides several ways to skin this cat, and not all are
so efficient.

I usually find it more reliable to edit the components of the series
formula directly, rather than build a new formula. Something like this:

With ActiveChart.SeriesCollection(3)
.Values = worksheets("RangeData").Range("rngACWP")
.XValues = worksheets("RangeData").Range("rngDate")
End With

Although I just noticed that this changed the name to the static
address. Try this:

With ActiveChart.SeriesCollection(3)
.Values = "='" & worksheets("RangeData").Name & "'!" & "rngACWP"
.XValues = "='" & worksheets("RangeData").Name & "'!" & "rngDate"
End With

If the name is a workbook level name, Excel changes the sheet name
prefix to the workbook name. If it is a worksheet level name, then Excel
leaves the sheet name intact.

- Jon
 
R

reynoldscm

John,
Thanks for the help. It worked wonderfully! I did encounter one proble
however. When there is no data in one of the series of the chart (in m
case I have #N/A when no data is available) the macro stops running. I
this just a simple error handling opportunity or would there be som
other piece of code that would allow it to continue processing?

Chris


Jon said:
*Chris -

As usual, Excel provides several ways to skin this cat, and not al
are
so efficient.

I usually find it more reliable to edit the components of the series
formula directly, rather than build a new formula. Something lik
this:

With ActiveChart.SeriesCollection(3)
..Values = worksheets("RangeData").Range("rngACWP")
..XValues = worksheets("RangeData").Range("rngDate")
End With

Although I just noticed that this changed the name to the static
address. Try this:

With ActiveChart.SeriesCollection(3)
..Values = "='" & worksheets("RangeData").Name & "'!" & "rngACWP"
..XValues = "='" & worksheets("RangeData").Name & "'!" & "rngDate"
End With

If the name is a workbook level name, Excel changes the sheet name
prefix to the workbook name. If it is a worksheet level name, the
Excel
leaves the sheet name intact.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
http://PeltierTech.com/Excel/Charts/
_______
[/QUOTE]


-
reynoldsc
 
J

Jon Peltier

Chris -

Yes, the NA() thing works very well, except when it doesn't. The NA()
lets you skip over points in a series, but if a chart series has only
blanks or only #N/A (or I guess a combination of the two), it isn't
charted, and VBA isn't as smart about some things as the regular Excel UI.

You have two choices. First, your macro can put a dummy piece of data in
the data range, so the series is temporarily charted while you adjust
the ranges. When you're done, put the #N/A back. Second, you could
temporarily change the chart type of the series to an Area chart, which
treats #N/A as a zero. Adjust the series, then change the chart type back.

- Jon
 

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