【问题标题】:Update colours of pie chart segments to the cell fill colours将饼图段的颜色更新为单元格填充颜色
【发布时间】:2019-11-13 15:54:40
【问题描述】:

我正在尝试使用 Excel 2016 根据从中提取数据的单元格为我的饼图段着色。

我从 YouTube video 中截取了代码,但这在某些地方很难阅读(1、l 和 i 特别难以区分),所以我不相信我的理解是正确的。

Private Sub SheetActivate(ByVal Sh As Object)
Dim cht As ChartObject
Dim i As Integer
Dim vntValues As Variant
Dim s As String
Dim mySeries As Series

For Each cht In ActiveSheet.ChartObjects

    For Each mySeries In cht.Chart.SeriesCollection

        If mySeries.ChartType <> xlPie Then GoTo SkipNotPie

        s = Split(mySeries.Formula, ",")(2)
        vntValues = mySeries.Values

        For i = 1 To UBound(vntValues)
            mySeries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
        Next l
SkipNotPie:
    Next mySeries
Next cht

End Sub

更新:这是显示图表的片段 - 我正在尝试更新图表段以表示第二列中的单元格填充颜色。

【问题讨论】:

  • 您好,感谢您的回复。我打开VB时没有显示任何错误。我将尝试分享这些图表的图片,以便澄清我正在尝试做的事情。

标签: excel vba colors pie-chart


【解决方案1】:

稍作调整,效果很好:

Private Sub SheetActivate()
    Dim cht As ChartObject
    Dim i As Long
    Dim vntValues As Variant
    Dim s As String
    Dim mySeries As Series

    For Each cht In ActiveSheet.ChartObjects

        For Each mySeries In cht.Chart.SeriesCollection

            If mySeries.ChartType <> xlPie Then GoTo SkipNotPie

            s = Split(mySeries.Formula, ",")(2)
            vntValues = mySeries.Values

            For i = 1 To UBound(vntValues)
                mySeries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
            Next i
SkipNotPie:
        Next mySeries
    Next cht
End Sub

确保模块顶部没有 Option Base 1。如果你这样做了,那就改变

            s = Split(mySeries.Formula, ",")(2)

            s = Split(mySeries.Formula, ",")(3)

自从我学会从零开始计数后,我就没有使用过Option Base 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2014-11-12
    • 2018-04-11
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多