Solved

create line charts with non-contiguous ranges using VBA

Posted on 2014-10-24
4
180 Views
Last Modified: 2014-10-26
Dear Experts:

I tried to create a line chart with two data series (two lines) using VBA:

Sub CreateLineChart_non_contiguous_range()
ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Range( _
        "Tabelle1!$A$5:$E$5;Tabelle1!$A$7:$E$7")
End Sub

Open in new window


The code should produce this chart, but it throws an 1004 error message on 'SetSourceData'

line chart
How is the macro to be re-written on the setSourceData line for it to work.

Help is much appreciated. Thank you very much in advance.

I have attached my sample file for your convenience.


Regards, Andreas
Create-line-chart-non-contiguous-ranges.
0
Comment
Question by:AndreasHermle
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 40402262
A small change required:

Sub CreateLineChart_non_contiguous_range()
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Sheets("Tabelle1").Range("A5:E5,A7:E7")
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40402279
Rory, great this did the trick. Thank you very much for your swift and professional help.

Regards, andreas
0
 
LVL 1

Expert Comment

by:Jon_Peltier
ID: 40404090
Probably the semicolon in the SetSourceData statement was the problem. VBA uses commas regardless of the regionalization of Excel.
0
 

Author Comment

by:AndreasHermle
ID: 40404908
Hi jon,  

indeed you are right. Thank you very much for bringing this to my attention. I really appreciate this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

867 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

16 Experts available now in Live!

Get 1:1 Help Now