|
-
Mar 20th, 2009, 10:56 AM
#1
Thread Starter
Hyperactive Member
[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:
For Each oXLChart In oXLBookCurrent.Charts
With oXLChart
iRow = oXLSheetBase.UsedRange.Rows.Count
With .SeriesCollection.NewSeries
.XValues = oXLSheetBase.Range("A3:A" & iRow)
.Values = oXLSheetBase.Range("B3:B" & iRow) 'This line needs to change
.Name = aBase(iBase)
.MarkerStyle = xlMarkerStyleNone
End With
End With
Next
-
Mar 20th, 2009, 11:13 AM
#2
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
-
Mar 20th, 2009, 11:21 AM
#3
Thread Starter
Hyperactive Member
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:
Private Sub btnImportBase_Click()
Dim oXLApp As Excel.Application
Dim oXLBookTemp As Excel.Workbook
Dim oXLBookCurrent As Excel.Workbook
Dim oXLSheetControl As Excel.Worksheet
Dim oXLSheetBase As Excel.Worksheet
Dim oXLSheetTemp As Excel.Worksheet
Dim oXLChart As Excel.Chart
Dim iBase As Integer
Dim sBase As String
Dim aBase(5) As String
Dim iCol As Integer
Dim iRow As Integer
Dim iSeriesCnt As Integer
aBase(1) = "Base1"
aBase(2) = "Base2"
aBase(3) = "Base3"
aBase(4) = "Base4"
aBase(5) = "Base5"
Set oXLBookCurrent = ThisWorkbook
Set oXLSheetControl = oXLBookCurrent.Worksheets("Control")
Set oXLApp = New Excel.Application
oXLApp.Visible = False 'Visible for debug only
oXLApp.DisplayAlerts = False
For iBase = 1 To 5
sBase = oXLSheetControl.Cells(8 + iBase, 2).Value 'Contains path of file to open
If Not sBase = "" Then 'If path exists
Set oXLBookTemp = oXLApp.Workbooks.Open(sBase) 'Open CSV file
Set oXLSheetTemp = oXLBookTemp.Worksheets(1) 'Define worksheet
'define worksheet in current book based on index iBase
Set oXLSheetBase = oXLBookCurrent.Worksheets(aBase(iBase))
oXLSheetTemp.UsedRange.Copy
oXLSheetBase.Range("A1").PasteSpecial Paste:=xlPasteValues
For Each oXLChart In oXLBookCurrent.Charts
With oXLChart
iRow = oXLSheetBase.UsedRange.Rows.Count
With .SeriesCollection.NewSeries
.XValues = oXLSheetBase.Range("A3:A" & iRow)
.Values = oXLSheetBase.Range(Cells(3, 2), Cells(iRow, 2))
.Name = aBase(iBase)
.MarkerStyle = xlMarkerStyleNone
End With
End With
Next
Else
oXLBookCurrent.Worksheets(aBase(iBase)).Visible = False
End If
Next iBase
oXLApp.Quit
Set oXLSheetTemp = Nothing
Set oXLSheetBase = Nothing
Set oXLBookCurrent = Nothing
Set oXLBookTemp = Nothing
Set oXLApp = Nothing
End Sub
-
Mar 20th, 2009, 11:26 AM
#4
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
-
Mar 20th, 2009, 12:24 PM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|