Results 1 to 5 of 5

Thread: [RESOLVED] [Excel 2003] Increment New Chart Series

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] [Excel 2003] Increment New Chart Series

    I have a workbook that already contains numerous charts. I now want to add a new series to each chart. The source data for the new series will come from a worksheet in which the columns align with the charts. for example the first chart will use column B for its source, the second will use column C for its source and so on. All charts will use column A as the x axis data.

    The code below works for the first chart but I can't figure out how to increment to the next columns for the remaining charts. Any ideas?


    vb Code:
    1. For Each oXLChart In oXLBookCurrent.Charts
    2.             With oXLChart
    3.                 iRow = oXLSheetBase.UsedRange.Rows.Count
    4.                 With .SeriesCollection.NewSeries
    5.                     .XValues = oXLSheetBase.Range("A3:A" & iRow)
    6.                     .Values = oXLSheetBase.Range("B3:B" & iRow) 'This line needs to change
    7.                     .Name = aBase(iBase)
    8.                     .MarkerStyle = xlMarkerStyleNone
    9.                 End With
    10.             End With
    11.         Next

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel 2003] Increment New Chart Series

    Instead of using Range("B3:B" & iRow), use the cells notation and then put then in the loop for example

    Code:
    'Range("B3:B" & iRow) can be written as
    Range(Cells(3, 2), Cells(iRow, 2))
    
    'Range("C3:C" & iRow) can be written as
    Range(Cells(3, 3), Cells(iRow, 3))
    
    'Range("D3:D" & iRow) can be written as
    Range(Cells(3, 4), Cells(iRow, 4))
    So in a loop it will look something like this

    Code:
    '~~> Looping from Col B to Col E for example
    For i = 2 To 5
        '~~ Rest of the code
        .Values = oXLSheetBase.Range(Cells(3, i), Cells(iRow, i))
        '~~ Rest of the code
    Next
    Last edited by Siddharth Rout; Mar 20th, 2009 at 11:16 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [Excel 2003] Increment New Chart Series

    I tried that already but get a run time error: "Application-defined or object-defined error". Here's the entire routine

    vb Code:
    1. Private Sub btnImportBase_Click()
    2.  
    3. Dim oXLApp As Excel.Application
    4. Dim oXLBookTemp As Excel.Workbook
    5. Dim oXLBookCurrent As Excel.Workbook
    6. Dim oXLSheetControl As Excel.Worksheet
    7. Dim oXLSheetBase As Excel.Worksheet
    8. Dim oXLSheetTemp As Excel.Worksheet
    9. Dim oXLChart As Excel.Chart
    10. Dim iBase As Integer
    11. Dim sBase As String
    12. Dim aBase(5) As String
    13. Dim iCol As Integer
    14. Dim iRow As Integer
    15. Dim iSeriesCnt As Integer
    16.  
    17. aBase(1) = "Base1"
    18. aBase(2) = "Base2"
    19. aBase(3) = "Base3"
    20. aBase(4) = "Base4"
    21. aBase(5) = "Base5"
    22.  
    23. Set oXLBookCurrent = ThisWorkbook
    24. Set oXLSheetControl = oXLBookCurrent.Worksheets("Control")
    25.  
    26. Set oXLApp = New Excel.Application
    27. oXLApp.Visible = False   'Visible for debug only
    28. oXLApp.DisplayAlerts = False
    29.  
    30. For iBase = 1 To 5
    31.     sBase = oXLSheetControl.Cells(8 + iBase, 2).Value  'Contains path of file to open
    32.     If Not sBase = "" Then   'If path exists
    33.            
    34.         Set oXLBookTemp = oXLApp.Workbooks.Open(sBase)  'Open CSV file
    35.         Set oXLSheetTemp = oXLBookTemp.Worksheets(1)    'Define worksheet
    36.        
    37.         'define worksheet in current book based on index iBase
    38.         Set oXLSheetBase = oXLBookCurrent.Worksheets(aBase(iBase))
    39.        
    40.         oXLSheetTemp.UsedRange.Copy
    41.         oXLSheetBase.Range("A1").PasteSpecial Paste:=xlPasteValues
    42.        
    43.         For Each oXLChart In oXLBookCurrent.Charts
    44.             With oXLChart
    45.                 iRow = oXLSheetBase.UsedRange.Rows.Count
    46.                 With .SeriesCollection.NewSeries
    47.                     .XValues = oXLSheetBase.Range("A3:A" & iRow)
    48.                     .Values = oXLSheetBase.Range(Cells(3, 2), Cells(iRow, 2))
    49.                     .Name = aBase(iBase)
    50.                     .MarkerStyle = xlMarkerStyleNone
    51.                 End With
    52.             End With
    53.         Next
    54.    
    55.     Else
    56.    
    57.         oXLBookCurrent.Worksheets(aBase(iBase)).Visible = False
    58.        
    59.     End If
    60. Next iBase
    61.  
    62. oXLApp.Quit
    63.  
    64. Set oXLSheetTemp = Nothing
    65. Set oXLSheetBase = Nothing
    66. Set oXLBookCurrent = Nothing
    67. Set oXLBookTemp = Nothing
    68. Set oXLApp = Nothing
    69.  
    70. End Sub

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel 2003] Increment New Chart Series

    Ok Try this

    Code:
    .Values = oXLSheetBase.Range(Replace(oXLSheetBase.Cells(3, 2).Address, "$", ""), _
                Replace(oXLSheetBase.Cells(iRow, 2).Address, "$", ""))
    Last edited by Siddharth Rout; Mar 20th, 2009 at 11:48 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [Excel 2003] Increment New Chart Series

    Hmmm. That got it but I have no idea why. I guess that there were unneeded "$" characters ??

    Thanks for the help.

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