I am trying to re-write some code created with the macro recorder whilst formatting a line on a chart. I have tried numerous ways of re-writing the syntax so i can use a With
statement to avoid any .Activate
or .Select
.
My code is failing with error 438 Object doesn't support this property of method
on the line:
ser.FormatLine.ForeColor.RGB = RGB(0, 176, 80)
Clearly .Format.Line
does not belong to mychart.SeriesCollection(10)
but after having looked across this site and various others trying to get the syntax right to enable me to use .Format.Line
i have failed. In particular, i haven't been able to find a matching example using a With
statement.
I believe i may need to access the Series not the SeriesCollection?
If it helps the particular series name is "Target", which is associated with SeriesCollection(10).
Hopefully, it is a simple fix and i would welcome any pointers on how to achieve my goal.
My latest code re-write was:
Public Sub FormatBenchTargetLine()
Dim wb As Workbook
Dim ws As Worksheet
Dim ser As Series
Set wb = ThisWorkbook
Set ws = wb.Sheets("Benchmarking")
Dim mychart As Chart
Set mychart = ws.ChartObjects("Chart 7").Chart
With ws
' On Error Resume Next
Set ser = mychart.SeriesCollection(10)
ser.Format.Line.Visible = msoTrue
ser.FormatLine.ForeColor.RGB = RGB(0, 176, 80) '***Errors here*******
ser.FormatLine.Transparency = 0
ser.FormatLine.DashStyle = msoLineSysDash
' On Error GoTo 0
End With
End Sub
Original macro code:
Sub FormatBenchTarg()
ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.FullSeriesCollection(10).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
End With
ActiveChart.ChartArea.Select
With Selection.Format.Line
.Visible = msoTrue
.DashStyle = msoLineDash
End With
End Sub