Click here to Skip to main content
16,018,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to graph two lines from this data
Here is my excel spreadsheet ----
sheet 1
time	value1
0	6
1	6
2	7
3	6
4	5
5	6
6	7
7	6
8	5
9	4
10	3
sheet 2------------------------------
time	value2
0	5
1	5
2	6
3	5
4	4
5	5
6	6
7	5
8	4
9	3
10	2


What I have tried:

Here is the code that generates one graph line from sheet 1

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xlApp As New Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp.Visible = True
        xlWorkBook = xlApp.Workbooks.Open("L:\x_GGNS_addons\WorkingJunk\book1.xlsx")
        xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)

        Dim xlcharts = xlWorkSheet.ChartObjects
        Dim myChart As Excel.ChartObject = xlcharts.Add(180, 80, 300, 250)
        Dim chartPage As Excel.Chart = myChart.Chart
        chartPage.Activate()


        With chartPage
            Dim range1 As Excel.Range
            Dim range2 As Excel.Range
            Dim chartRange As Excel.Range
            'range1 = xlWorkSheet.Range("$A$1", "$A$10")
            range1 = xlWorkSheet.Range("sheet1!$A$1:$A$10")
            'range2 = xlWorkSheet.Range("$B$1", "$B$10")
            range2 = xlWorkSheet.Range("sheet1!$B$1:$B$10")
            chartRange = xlApp.Union(range1, range2)
            .SetSourceData(chartRange, Excel.XlRowCol.xlColumns)
            .ApplyCustomType(Excel.XlChartType.xlXYScatterLines)
            .Location(Excel.XlChartLocation.xlLocationAsNewSheet, "chart1")
          
        End With
    End Sub
End Class

The code above generates the graph as expected.
Here is the macro that adds the second line
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveChart.ChartArea.Select
    ActiveChart.PlotArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(2).XValues = "=Sheet2!$A$2:$A$12"
    ActiveChart.FullSeriesCollection(2).Values = "=Sheet2!$B$2:$B$12"
    ActiveChart.FullSeriesCollection(2).Name = "=Sheet2!$A$2:$A$12"
    ActiveChart.FullSeriesCollection(2).Name = "=Sheet2!$B$1"
End Sub

For the life of me I cannot figure out how to add the second line in Visual basic!
HELP!
Robert
Posted
Updated 28-Feb-18 8:58am
Comments
Maciej Los 28-Feb-18 10:59am    
DO you want to create chart object inside worksheet, or you want to create a sheet-chart?
Robert Goldman 28-Feb-18 12:49pm    
I created a chart called "chart1" with the line from sheet1. I want to add a second line to "chart1" from the data from sheet2.

1 solution

As i mentioned in my comment to the question, there are 2 types of charts in Excel: embedded chart and a chart sheet. See: Excel Dashboard Templates What is an Excel chart sheet vs. an embedded chart? - Excel Dashboard Templates[^]

Depending on that, the method of adding source of chart lines/curves may differ.
See: Creating Charts in Microsoft Office Excel 2003 Using Visual Basic for Applications Code[^]

Good luck!

[EDIT]

Try this:
VB.NET
 Dim xlApp As Excel.Application, xlWbk As Excel.Workbook, xlChart As Excel.Chart
 Dim sFileName As String = "FullPathAndFileName.xlsx" '

 Try
     xlApp = New Excel.Application
     ''uncomment below line if workbook is close
     xlWbk = xlApp.Workbooks.Open(sFileName)
     'Create a new chart.
     xlChart = xlWbk.Charts.Add()
     With xlChart
         .Name = "MyChart_" & xlWbk.Charts.Count
         .ChartType = 4 'xlLine
         .HasTitle = True
         .ChartTitle.Text = "Time And Value"
         .SeriesCollection(1).Name = "=Sheet1!$B$1"
         .SeriesCollection(1).Values = "=Sheet1!$B$2:$B$12"
         .SeriesCollection(2).Name = "=Sheet2!$B$1"
         .SeriesCollection(2).Values = "=Sheet2!$B$2:$B$12"
         .Axes(1, 1).HasTitle = True
         .Axes(1, 1).AxisTitle.Characters.Text = "Time"
         .Axes(2, 1).HasTitle = True
         .Axes(2, 1).AxisTitle.Characters.Text = "Value"

     End With
     xlApp.Visible = True

 Catch ex As Exception
     Console.WriteLine("{0}", ex.Message)

 Finally
     xlChart = Nothing
     xlWbk = Nothing
     xlApp = Nothing
End Try

Above code draws a chart with 2 lines ;)
 
Share this answer
 
v2
Comments
Robert Goldman 28-Feb-18 15:05pm    
It a sheet-chart. I can add the second line using a Macro but I cannot do it using the visual basic. It appears the link you gave me was for VBA. Am I missing something?
I cannot convert the VBA macro code to Visual basic code.
Robert Goldman 28-Feb-18 15:05pm    
Thanks for you help!
Maciej Los 28-Feb-18 17:36pm    
Check updated answer (after [EDIT] section).
Robert Goldman 28-Feb-18 18:04pm    
when the code hits ---
.SeriesCollection(2).Name = "=Sheet2!$B$1"

It gets an exception -- Parameter not valid
Any ideas?
Thanks for your help!
Robert
Maciej Los 1-Mar-18 1:49am    
I tested above code and it works fine. Seems, Excel isn't able to create second line. Try to use SeriesCollection.NewSeries method just before the line which generates an error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900