Solved

create line charts with non-contiguous ranges using VBA

Posted on 2014-10-24
4
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

624 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