如何有条件地设置 Excel SmartArt 图形的格式

How to Conditionally Format an Excel SmartArt graphic

我在 Excel 2016 年做一个项目,需要做的是一个图表(最好是一个圆圈)根据分数改变每个 "slice's" 颜色。分数是从 excel sheet 中提取的整数,因此它会根据调查结果发生变化。

分数从 1 到 5,其中 1 是红色,5 是绿色。我知道如何有条件地将单元格本身格式化为我需要的颜色,但我不知道如何在图表上这样做。

我查看了 VBA(通过 YouTube 视频),但我也无法让它发挥作用。

这是我为 VBA 准备的代码,如果有人能帮助我,或者让我知道怎么做,那就太好了!

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).Interiror.Color
        Next i

SkipNotPie:
    Next mySeries
Next cht

End Sub

您输入错误

范围(s).单元格(i).Interiror.Color

您的代码中未使用子过程变量。

Sub test()
    SheetActivate Activesheet '<~~ This will execute the following procedure.

End Sub

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 Sh.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

如果您的单元格颜色是有条件格式化的,则像这样更改

For i = 1 To UBound(vntValues)
    'mySeries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
    mySeries.Points(i).Interior.Color = Range(s).Cells(i).FormatConditions(1).Interior.Color
Next i