Excel VBA线条颜色/标记线条颜色

Bob*_*ith 9 excel charts vba

我正在编写一些VBA代码来修改Excel图表.对于散点图,我需要修改标记线颜色,有时还需要修改连接线的线条颜色.我可以手动完成,但是当我录制宏时,两个动作都会产生相同的代码,尽管结果非常不同.

知道如何区分代码中的线条颜色和标记线颜色吗?

当我记录自己改变标记线的颜色时,创建了此代码

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
Run Code Online (Sandbox Code Playgroud)

当我记录自己改变连接标记的线的颜色时,创建了此代码

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
Run Code Online (Sandbox Code Playgroud)

Axe*_*ter 18

连线的线条颜色是Series.Format.Line.ForeColor.标记线颜色是Series.MarkerForegroundColor.但至少在Excel 2007中存在设置问题Series.Format.Line.ForeColor.见例子:

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
Run Code Online (Sandbox Code Playgroud)

ActiveChart是散点图.这是使用Excel 2007测试的.


Ste*_*lor 7

从 Excel 2013 开始,线条颜色和标记线颜色很容易区分,因为线条颜色是使用.Border属性设置的,而标记颜色是使用.MarkerBackgroundColor.MarkerForegroundColor属性设置的。

因此,以下将为您提供白色标记,它们之间带有红色边框和黑色连接线:

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
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用Selection.Format.Line.Weight,请注意这默认适用于边框和连接线粗细

  • `.Border`、`.MarkerBackgroundColor` 和 `.MarkerForegroundColor` 是旧 Excel 97-2003 对象模型的遗留部分。它们应该被替换为 `LineFormat` 和 `FillFormat`,但是这些在 Office 2007 中没有完全实现,在 Office 2016 中仍然没有得到更正。 (2认同)