
Originally Posted by
hegdep
What is the syntax I must use to run this code with ActiveSheet instead of using Sheet 1? Especially to replace "=Sheet1!$D$"
Thanks for your help?
I guess this will do what you need.
.Name = "='" & activesheet.name & "'!$D$" & iCount
If you wanted to create a new chart, and name it relative to the sheet name, you'd need somecode like this at the start of the routine...
Dim cNewChart As ChartObject
Set cNewChart = ActiveSheet.ChartObjects.Add(Left:=100, Width:=375, Top:=50, Height:=300)
cNewChart.Chart.ChartType = xlXYScatterLines
cNewChart.Name = "Chart " & ActiveSheet.Name
To loop through all worksheets, wrap a for....each....in.....next loop around the code
and refer to the sheet ws in the code
Dim ws As Worksheet
For Each ws In Worksheets
'
'
'
'
'
Next
Altogether, this might be work for you.
Sub PlotCharts()
Dim iLastRow As Integer, iCount As Integer
Dim ws As Worksheet
Dim cNewChart As ChartObject
For Each ws In Worksheets
iLastRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Offset(1, 0).Row
Set cNewChart = ws.ChartObjects.Add(Left:=100, Width:=375, Top:=50, Height:=300)
cNewChart.Chart.ChartType = xlXYScatterLines
cNewChart.Name = "Chart " & ws.Name
For iCount = 1 To iLastRow Step 24
cNewChart.Activate
With ActiveChart.SeriesCollection.NewSeries
.Name = "='" & ws.Name & "'!$D$" & iCount
.XValues = "'" & ws.Name & "'!$C$" & iCount + 3 & ":$C$" & iCount + 22
.Values = "'" & ws.Name & "'!$D$" & iCount + 3 & ":$D$" & iCount + 22
End With
Next iCount
ActiveChart.SetElement (msoElementLegendRight)
Next ws
End Sub
Bookmarks