VBA: Extracting the RGB value of lines in a chart with default colors

前端 未结 3 1868
抹茶落季
抹茶落季 2021-02-08 11:09

Problem

I would like to know how to read the current RGB value of an automatically assigned color in a chart, even if this entails freezing the colors to their current

3条回答
  •  花落未央
    2021-02-08 12:03

    Here is the code I used in the end.

    Sub ShowSeries()
    Dim mySrs As Series
    Dim myPts As Points
    Dim chtType As Long
    Dim colors As String
    
    With ActiveSheet
        For Each mySrs In ActiveChart.SeriesCollection
            'Add label
            Set myPts = mySrs.Points
            myPts(myPts.Count).ApplyDataLabels ShowSeriesName:=True, ShowValue:=False
    
            'Color text label same as line color
    
            'if line has default color
            If mySrs.Border.ColorIndex = -4105 Then
                chtType = mySrs.ChartType
                'Temporarily turn this in to a column chart:
                mySrs.ChartType = 51
                mySrs.DataLabels.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = _
                    mySrs.Format.Fill.ForeColor.RGB
                'reset the chart type to its original state:
                mySrs.ChartType = chtType
    
            'if line has a color manually changed by user
            Else
                mySrs.DataLabels.Font.ColorIndex = mySrs.Border.ColorIndex
            End If
        Next
    End With
    

    End Sub

提交回复
热议问题