【问题标题】:Set Chart Series Colors to Match Category Cell Colors VBA设置图表系列颜色以匹配类别单元格颜色 VBA
【发布时间】:2016-10-06 11:32:11
【问题描述】:

我需要将我的类别背景颜色与折线图系列颜色匹配的 VBA 宏。 现在我没有使用最好的方法,因为我正在应用以下代码

它将图表系列颜色设置为与源单元格颜色相同。 (图片上的例子)

但我希望这个宏从类别的单元格(代表 2009、2010、2011 年)而不是源单元格中获取颜色。

我找不到简单直接的方法。我正在宏设置源单元格的背景颜色以匹配类别颜色,然后我将白色放在具有条件格式的源单元格顶部。所以只有类别是彩色的,源单元格是白色的。

想知道是否有更好的方法。 (图片上的最终结果,类别名称匹配系列颜色)

Dim oChart As ChartObject
Dim MySeries As Series
Dim FormulaSplit As Variant
Dim SourceRange As Range
Dim SourceRangeColor As Long

'Loop through all charts in the active sheet
For Each oChart In ActiveSheet.ChartObjects

    'Loop through all series in the target chart
   For Each MySeries In oChart.Chart.SeriesCollection

        'Get Source Data Range for the target series
       FormulaSplit = Split(MySeries.Formula, ",")

        'Capture the first cell in the source range then trap the color
       Set SourceRange = Range(FormulaSplit(2)).Item(1)
        SourceRangeColor = SourceRange.Interior.Color

        On Error Resume Next
        'Coloring for Excel 2003
       MySeries.Interior.Color = SourceRangeColor
        MySeries.Border.Color = SourceRangeColor
        MySeries.MarkerBackgroundColorIndex = SourceRangeColor
        MySeries.MarkerForegroundColorIndex = SourceRangeColor

        'Coloring for Excel 2007 and 2010
       MySeries.MarkerBackgroundColor = SourceRangeColor
        MySeries.MarkerForegroundColor = SourceRangeColor
        MySeries.Format.Line.ForeColor.RGB = SourceRangeColor
        MySeries.Format.Line.BackColor.RGB = SourceRangeColor
        MySeries.Format.Fill.ForeColor.RGB = SourceRangeColor

    Next MySeries
Next oChart

End Sub

【问题讨论】:

  • 不清楚你在追求什么。您能否发布您想要的示例屏幕截图,并更具体地说明您的代码正在做什么(或不做什么)?
  • 用一些照片和解释更新了主帖

标签: excel vba charts


【解决方案1】:

假设我完全了解您的要求,那么您已经非常接近了。我认为您的代码中的问题是您如何拆分系列公式以获取标签颜色。

我翻了这张图表,列标题颜色如下:

使用此代码进入下面的图表:

Sub SetColors()

Dim oChart As ChartObject
Dim MySeries As Series

For Each oChart In ActiveSheet.ChartObjects

    For Each MySeries In oChart.Chart.SeriesCollection

        Dim sFormula As String
        sFormula = Split(MySeries.Formula, ",")(0) 'this returns the =SERIES(Sheet!RC part of the formula, the first argument is the series label
        sFormula = Split(sFormula, "(")(1) 'this removes the =SERIES(  leaving only the column label range (Sheet!RC)

        Dim lSourceColor As Long
        lSourceColor = Range(sFormula).Interior.Color

        With MySeries
            .Interior.Color = lSourceColor
            .Border.Color = lSourceColor
            '.MarkerBackgroundColorIndex = lSourceColor
            '.MarkerForegroundColorIndex = lSourceColor
            .MarkerBackgroundColor = lSourceColor
            .MarkerForegroundColor = lSourceColor
            With .Format.Line
                .ForeColor.RGB = lSourceColor
                .BackColor.RGB = lSourceColor
            End With
            .Format.Fill.ForeColor.RGB = lSourceColor
        End With

    Next

Next

End Sub

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 2018-06-02
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2019-03-20
    • 2018-04-24
    相关资源
    最近更新 更多