没有隐藏单元格的单元格颜色的颜色图表系列

Color Chart Series by Cell Color without hidden Cells

我正在尝试使用特定工作表上活动单元格的单元格颜色创建图表。使用下面提供的宏时,一切正常。颜色与单元格匹配。 但是,当隐藏或过滤某些行时,这个宏没有 运行 正确,为什么???? 请帮助我

Sub ColorChartbyCellColor()
With Sheets("Chart1").SeriesCollection(1)
Set vAddress = Sheets("Sheet1").Range(Split(Split(.Formula, ",")(1), "!")(1))
For i = 1 To vAddress.Cells.Count
    .Points(i).Format.Fill.ForeColor.RGB = ThisWorkbook.Colors(vAddress.Cells(i).Interior.ColorIndex)
    Next i
End With
End Sub

如果您过滤数据,那么您的图表系列的点数会减少,但源范围的大小仍然相同。 因此,当您遍历 vAddress 时,您最终 运行 无法设置颜色(您设置的可能是错误的)。

您只需要在循环时计算可见行数:

Sub ColorChartbyCellColor()

    Dim vAddress As Range, n As Long, c As Range

    With Sheets("Chart1").SeriesCollection(1)

        Set vAddress = Sheets("Sheet1").Range(Split(Split(.Formula, ",")(1), "!")(1))
        n = 0 'for counting visible datapoints
        For Each c In vAddress.Cells
            'is the row visible? skip if not
            If Not c.EntireRow.Hidden Then
                n = n + 1 'next visible datapoint
                .Points(n).Format.Fill.BackColor.RGB = c.Interior.Color
            End If
        Next c
    End With

End Sub

使用特定工作表上活动单元格的单元格颜色创建图表。当使用下面提供的这个宏时,一切都完美无缺。

Sub ColorChartbyCellColor()

Dim vAddress As Range, n As Long, c As Range

With Sheets("Chart1").SeriesCollection(1)

    Set vAddress = Sheets("Sheet1").Range(Split(Split(.Formula, ",")(1), "!")(1))
    n = 0 'for counting visible datapoints
    For Each c In vAddress.Cells
        'is the row visible? skip if not
        If Not c.EntireRow.Hidden Then
            n = n + 1 'next visible datapoint
            .Points(n).Format.Fill.ForeColor.RGB = c.Interior.Color
        End If
    Next c
End With

结束子