Problem with using R and C in charting macro

ajwillshire

New Member
Joined
Mar 4, 2009
Messages
31
Hi,

I've got a macro which creates dynamic ranges and adds them to a chart. As part of it a User Form offers the chance to prefix the Dynamic Name before charting.

However, if I use a prefix beginning with R or C it falls over. I know that R or C followed by an integer is forbidden but as far as i can see that doesn't occur.

Here's the line which trips it up:

ActiveChart.SeriesCollection.NewSeries.Formula = "=SERIES(" & Chr(34) & mySeriesName & Chr(34) & "," & wksName & "!" & Prefix_ns & "Dates," & wksName & "!" & myName & "," & i & ")"

mySeriesName is the name that appears in the legend - say "October Spend".
wksName is the worksheet name - "Sheet2" in my example
Prefix_ns is the prefix e.g., "Roger_"
myName is the name of the dynamic range - "October_Spend"
i is the index of the loop to give the series number in the chart.


So the formula that it should be is
"=Series("October Spend", 'Sheet2'!Roger_Dates, '"Sheet2'!Roger_October_Spend, 1)

But it doesn't work if the prefix begins with R or C, So Roger, Rhys, Colin, etc all trip it up while all other names work perfectly.

Any ideas?
Thanks in advance,
Andrew
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try setting the parts of the newseries rather than the Formula

Code:
Sub zz()

    mySeriesName = "October Spend"
    wksName = "Sheet2"
    Prefix_ns = "Roger_"
    myName = "October_Spend"
    i = 1
    With ActiveChart.SeriesCollection.NewSeries
        .Name = mySeriesName
        .Values = "='" & wksName & "'!" & myName
        .XValues = "='" & wksName & "'!" & Prefix_ns & "Dates"
        .PlotOrder = i
    End With
    
End Sub
 
Upvote 0
Your code produces:
=SERIES("October Spend",Sheet2!Roger_Dates,Sheet2!October_Spend,1)
which is not what you said it produces.
 
Upvote 0
Hi Andy,

Thanks for your reply - it works perfectly!

(Except for the .plotorder line - that seems only to be a property of "series" and not "newseries" - but it's not important for my needs).

Thanks for your time,
Andrew
 
Upvote 0
Hi Glenn,

You're right! But it was my mistake in writing it in this posting. myName is prefixed previously in the code so it is actually "Roger_October_Spend" by the time it gets to the chart formula.

Thanks for your time though. I'm still baffled by why the names beginning with R and C threw errors but Andy's fix works fine.

Cheers,

Andrew
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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