【问题标题】:Changing series colors by series name in a Chart with VBA使用 VBA 在图表中按系列名称更改系列颜色
【发布时间】:2021-12-24 17:24:25
【问题描述】:

我很难使用包含多个系列(目前超过 170 个)的折线图。我想根据系列名称上出现的几个字母为每个系列着色 - 系列名称可能仅包含这些字母以外的其他信息。

假设我想将包含字母 ABC(按此特定顺序)的每个系列着色为红色,将包含 DEF 的系列着色为黑色,将包含 GHI 的系列着色为蓝色。

我知道一些编程基础知识(主要是 PHP 和 Python),但我对 VBA 不是很熟悉。使用以下代码行,我可以将系列的颜色从 1 更改为 10。我该如何表达 for 循环,使其只处理系列名称中包含某些字母单词的系列?

Dim j As Long
     With ActiveSheet.ChartObjects("Chart 1").Chart
        For j = 1 To 10
            With .SeriesCollection(j)
                .MarkerStyle = 2
                .MarkerSize = 7
                .Format.Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Format.Line.Visible = msoFalse
            End With
        Next j
    End With
End Sub

【问题讨论】:

    标签: excel vba for-loop excel-charts


    【解决方案1】:

    我想我可以自己解决这个问题。我在 For 循环中使用了 If-condition,并使用 InStr() 函数来检查系列名称 (ser.Name) 是否包含想要的字母。我希望这可以帮助其他遇到同样问题的人。请看下文。

    Dim ser As Series
     
        With ActiveSheet.ChartObjects("Chart 1").Chart
            For Each ser In .SeriesCollection
                With ser
                    If InStr(ser.Name, "ABC") Then
                        .MarkerStyle = 2
                        .MarkerSize = 7
                        .Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
                        .Format.Line.Visible = msoFalse
                    End If
                End With
            Next ser
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多