Results 1 to 6 of 6

Thread: Excel Graphs and VB

  1. #1

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309

    Excel Graphs and VB

    I have a program that creates an Excel spreadsheet and creates a line graph on the spreadsheet. It works great except I need to add another line to the graph. How do I do that? Here is the code I use to create everything:
    VB Code:
    1. Dim objExcel As Excel.Application
    2. Dim objWorkBook As Excel.Workbook
    3. Dim objWorkSheet As Excel.Worksheet
    4. Dim objXLRange As Excel.Range
    5. Dim objXLChart As Excel.Chart
    6.  
    7. Set objExcel = CreateObject("Excel.Application")
    8. Set objWorkBook = objExcel.Workbooks.Add
    9. Set objWorkSheet = objWorkBook.Worksheets(1)
    10.  
    11. Set objXLRange = objWorkSheet.Range("E2", RangeX)
    12. Set objXLChart = objWorkSheet.Application.Charts.Add
    13. With objXLChart
    14.     .ChartType = xlLine
    15.     .SetSourceData objXLRange, xlColumns
    16. End With

    This is not all the code understand, just enough to let you know how I am doing it all.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Just use the macro-recorder and look what code that generates, copy-adjust-past.

  3. #3

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    Because I'm no Excel expert and have no idea of how to do that.

  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    It is quite easy, start the excel macro-recorder (Alt-F8).
    Do what you want to do (add the line to the chart) just how you do it normally.
    Stop the macro-recorder (it has a stop-button like a cd-player)
    Look into the code.

  5. #5

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    I don't know why this thread got moved to VBA because I'm not using VBA. I am using VB to do this. Anyway, this is the code I used:
    VB Code:
    1. 'add another series from your sheet called MySheet
    2. With objXLChart
    3.     .ChartArea.Select
    4.     i = .SeriesCollection.Count + 1
    5.     .SeriesCollection.NewSeries
    6.     .SeriesCollection(i).Name = "=Sheet1!R2C6"
    7.     .SeriesCollection(i).XValues = "=Sheet1!R2C6:" & RangeY & "C6"
    8.     .SeriesCollection(i).Values = "=Sheet1!R2C6:" & RangeY & "C6"
    9. End With

  6. #6
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    You may be using VB, but the language used to work in Excel is VBA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width