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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29675485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:32:09  来源:igfitidea点击:

Excel VBA Line Color / Marker Line Color

excelvbacharts

提问by Bob Smith

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.

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

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

回答by Axel Richter

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:

连接线的线条颜色为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

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

ActiveChart 是一个散点图。这是用 Excel 2007 测试的。

回答by Steve Taylor

From Excel 2013, the line colour and the marker line colour are easy to distinguish, as the Line colour is set using the .Borderproperty, whilst the Marker colours are set using .MarkerBackgroundColorand .MarkerForegroundColorproperties.

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

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

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

回答by Pramod Ghag

You could use

你可以用

ActiveChart.SeriesCollection(1).MarkerForegroundColor = -2

ActiveChart.SeriesCollection(1).MarkerForegroundColor = -2