如何有条件地格式化Excel SmartArt图形

时间:2018-06-08 14:30:47

标签: excel vba excel-vba formatting conditional

我正在使用Excel 2016中的一个项目,需要做的是一个图表(最好是一个圆圈),根据分数改变每个“切片”的颜色。分数是从Excel工作表中提取的整数,因此它将根据调查结果进行更改。

分数为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

1 个答案:

答案 0 :(得分:1)

你有错误输入

范围(S).Cells(ⅰ).Interiror.Color

您的代码中未使用

和子过程变量。

     func reCheck() {
            if let servicesFieldId = servicesField.selectedItem?.id1, let brandsFieldId = brandsField.selectedItem2?.id2 {
                label.text = String(servicesFieldId) + String(format: "%02d%02d", brandsFieldId})
            }

enter image description here

如果你的细胞颜色有条件地形成,那么就像这样改变

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