Solved

create line charts with non-contiguous ranges using VBA

Posted on 2014-10-24
4
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
Probably the semicolon in the SetSourceData statement was the problem. VBA uses commas regardless of the regionalization of Excel.
0
 

Author Comment

by:AndreasHermle
Comment Utility
Hi jon,  

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

743 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

18 Experts available now in Live!

Get 1:1 Help Now