15

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
1
  • 1
    This is a shortcoming in the object model for charts. Using the .Format.Line syntax applies the same formatting to marker line and connecting line. Using .Border applies the color to the connecting line and .MarkerForegroundColor to the marker line. Apr 19, 2015 at 17:41

3 Answers 3

27

The line colour of the connecting lines is Series.Format.Line.ForeColor. The marker line colour is Series.MarkerForegroundColor. But at least with Excel 2007 there is a problem with setting Series.Format.Line.ForeColor. See example:

Sub Macro3()
 Dim oChart As Chart
 Dim oSeries As Series

 Set oChart = ActiveChart
 Set oSeries = oChart.SeriesCollection(2)

 oSeries.Format.Line.Weight = 5 'Line.Weigth works ever

 oSeries.Format.Line.Visible = msoFalse 'for Line.ForeColor getting to work we have to cheat something
 oSeries.Format.Line.Visible = msoTrue
 oSeries.Format.Line.ForeColor.RGB = RGB(0, 255, 0) 'now it works

 oSeries.MarkerSize = 15
 oSeries.MarkerBackgroundColor = RGB(255, 0, 0) 'marker background

 oSeries.MarkerForegroundColor = RGB(0, 0, 255) 'marker foreground (lines around)
End Sub

The ActiveChart is a scatter chart. And this is tested with Excel 2007.

3
  • Thanks for the replies. @Axel, your suggested trick has worked for me too. Thanks.
    – Bob Smith
    Apr 16, 2015 at 14:50
  • Thank you so much from the bottom of my heart for the hack of .Line.Visible. I had the first series in a collection not showing the line (in Excel 2010). Adding msoFalse before msoTrue did the trick!
    – zoagli
    Jul 25, 2016 at 12:51
  • 1
    From Excel 2013 the line between markers is apparently set using the .Border property. Took me an hour of trying your advice before I realised what was happening! Nov 8, 2016 at 11:39
10

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:

ActiveChart.SeriesCollection(1).Select
With Selection
    .Border.LineStyle = xlContinuous
    .Border.Color = RGB(0,0,0)
    .MarkerBackgroundColor = RGB(255, 255, 255)
    .MarkerForegroundColor = RGB(255, 0, 0)
End With

NB: If you make use of Selection.Format.Line.Weight, note this applies to both the borders and connecting line thickness by default

1
  • 2
    .Border, .MarkerBackgroundColor, and .MarkerForegroundColor are legacy bits of the old Excel 97-2003 object model. They were supposed to be replaced by LineFormat and FillFormat, but these were incompletely implemented in Office 2007, and in Office 2016 have still not been corrected. Mar 31, 2017 at 15:46
0

You could use

ActiveChart.SeriesCollection(1).MarkerForegroundColor = -2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.