Tried and true code for creating chart suddenly doesn't work

I have a macro which starts by creating a chart from a non-contiguous range. I've run it hundreds of times without incident but today it suddenly balks, throwing an "Application-defined or object-defined error" on line 8. It works fine if the range is contiguous, but not if it is non-contiguous. What's going on and how do I fix it?

Set topB = [B1] 
Set topG = [G1]
Set btmB = topB.End(xlDown)
Set btmG = topG.End(xlDown)
Set rngB = Range(topB, btmB)
Set rngG = Range(topG, btmG)
Union(rngB, rngG).Select  
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Union(rngB, rngG)

Open in new window

Clueless,
John
LVL 1
John CarneyReliability Business Tools Analyst IIAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
I don't think that line 8 has anything which would produce an error as it is inserting a blank chart on the active sheet.
Rgonzo1971Commented:
Hi,

Do not select your range first

Set topB = [B1] 
Set topG = [G1]
Set btmB = topB.End(xlDown)
Set btmG = topG.End(xlDown)
Set rngB = Range(topB, btmB)
Set rngG = Range(topG, btmG)
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Union(rngB, rngG)

Open in new window

Regards
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
@Rgonzo
I don't think that will make any difference as the range will be deselected once the chart is added and selected.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Rgonzo1971Commented:
@sktneer On my machine it works that way

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Syntactically your suggestion is correct but again the code will work irrespective of machine.
John CarneyReliability Business Tools Analyst IIAuthor Commented:
I found the problem. I can't believe how non-intuitive it was. It was due to the fact that I had just changed the list separator in the Region and Language section of the Control Panel from a comma to a semi-colon. What!!!? I changed it back to a comma and everything worked fine. Go figure. One of the benefits of being ADD is that in the midst of all the things you forget or misplace, occasionally you suddenly remember where you put that needle in the haystack.

Thanks for your willingness to help!

John
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.