使条形图不同的颜色

时间:2015-09-15 20:17:52

标签: excel vba excel-vba

我有一个代码可以在条形图中更改条形图的颜色,但现在它给了我类型不匹配的功能。请帮我修改我的代码。

Sub UpdateChart()

Dim myChartObj As ChartObject
Dim myChart As Chart
Dim mySeries(1 To 10) As Series
Dim myChartFormat(1 To 10) As ChartFormat
Dim myFillFormat(1 To 10) As FillFormat
Dim myColorFormat(1 To 10) As ColorFormat

ActiveSheet.ChartObjects(1).Activate

Set myChart = ActiveChart

For i = 1 To 10

    Set mySeries(i) = myChart.SeriesCollection(i)
    Set myChartFormat(i) = mySeries(i).Format
    Set myFillFormat(i) = myChartFormat(i).Fill
    Set myColorFormat(i) = myFillFormat(i).ForeColor


     If i = 1 Then

    myColorFormat(i).RGB = getRGB1(Cells(12, 7))

    ElseIf i = 2 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 7))

    ElseIf i = 3 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 8))

    ElseIf i = 4 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 9))

    ElseIf i = 5 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 10))

    ElseIf i = 6 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 11))

    ElseIf i = 7 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 12))

    ElseIf i = 8 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 13))

    ElseIf i = 9 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 14))

    ElseIf i = 10 Then

     myColorFormat(i).RGB = getRGB1(Cells(12, 15))

     End If




Next i



End Sub

Function getRGB1(rcell) As String
    Dim sColor As String

    sColor = Right("000000" & Hex(rcell.Interior.Color), 6)
    getRGB1 = Right(sColor, 2) & Mid(sColor, 3, 2) & Left(sColor, 2)
End Function

1 个答案:

答案 0 :(得分:0)

在G12:R12

范围内设置每个单元格的颜色
Option Explicit

Sub UpdateChart()
    Dim myChart As Chart

    Set myChart = ActiveSheet.ChartObjects(1)
    For i = 1 To 10
        With myChart.SeriesCollection(1)
            .Points(i).Format.Fill.ForeColor.RGB = Cells(12, i + 6).Interior.Color
        End With
    End Sub
End Sub