首页 > 解决方案 > 如何通过 VBA 更改 Excel 图表中点的颜色

问题描述

由于我正在自动创建图表以比较测量数据和模拟数据,因此我希望每次有人使用脚本时数据看起来完全相同。

为此,我希望线条具有相同的颜色,例如蓝色始终是测量数据,绿色始终是模拟数据。我希望线上的所有内容都是相同的颜色,所以线以及数据都指向自己。此外,数据点有一个框架,我需要控制它的颜色或完全摆脱它。

现在,我使用以下代码:

'create first series (measurement)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = measurement_legend_title
ActiveChart.FullSeriesCollection(1).XValues = "=Peak_Torque_Values!$A$3:$A$30"
ActiveChart.FullSeriesCollection(1).Values = "=Peak_Torque_Values!$B$3:$B$30"
ActiveChart.FullSeriesCollection(1).Select

Selection.MarkerStyle = 1
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 240)
    .Solid
End With
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 240)
    .Transparency = 0
End With

结果如下所示:

现在的样子

谁能告诉我如何更改现在为深蓝色的标记的颜色?另外,有人能告诉我如何摆脱标记周围的框架吗?Excel 中的宏记录确实对我没有帮助。

感谢你们!!

编辑:第一个解决方案后仍然是“旧方式”的传说图片: 更新传奇

标签: excelvba

解决方案


这段代码会循环遍历数据点标记并将数据点标记更改为红色(可以通过更改代码中的 RBG() 函数来更改具体颜色。注意此代码引用了第一个系列。

Dim x As Integer
    For x = 1 To ActiveChart.FullSeriesCollection(1).Points.Count
        ActiveChart.FullSeriesCollection(1).Points(x).Select
        Selection.MarkerStyle = 2    'Sets the style/look of the marker
        With Selection.Format.Fill
            .ForeColor.RGB = RGB(255, 0, 0)  'determines the color of the marker
        End With
    Next x

推荐阅读