I'm writing some VBA code to modify Excel charts. For a scatter chart I need to modify the marker line colour and sometimes the line colour of the connecting lines. I can do both manually but when I record a Macro, both actions result in the same code despite the results being very different.
Any idea how to distinguish between a line colour and a marker line colour in code?
This code was created when I recorded myself changing colour of the marker lines
Sub Macro3()
'
' Macro3 Macro
'
'
ActiveChart.SeriesCollection(2).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
End Sub
This code was created when I recorded myself changing the color of the line connecting the markers
Sub Macro4()
'
' Macro4 Macro
'
'
'Change the Line Color
ActiveChart.SeriesCollection(2).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
End Sub
You could use
ActiveChart.SeriesCollection(1).MarkerForegroundColor = -2
The line colour of the connecting lines is
Series.Format.Line.ForeColor
. The marker line colour isSeries.MarkerForegroundColor
. But at least with Excel 2007 there is a problem with settingSeries.Format.Line.ForeColor
. See example:The ActiveChart is a scatter chart. And this is tested with Excel 2007.
From Excel 2013, the line colour and the marker line colour are easy to distinguish, as the Line colour is set using the .Border property, whilst the Marker colours are set using .MarkerBackgroundColor and .MarkerForegroundColor properties.
So the following will give you white markers, with a red border and black connecting lines between them:
NB: If you make use of Selection.Format.Line.Weight, note this applies to both the borders and connecting line thickness by default