【问题标题】:How to Conditionally Format an Excel SmartArt graphic如何有条件地格式化 Excel SmartArt 图形
【发布时间】:2018-11-18 15:32:06
【问题描述】:

我正在处理 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

【问题讨论】:

  • 这只是一个错字吗? Interior.color,不是Interiror

标签: vba excel formatting conditional


【解决方案1】:

你打错了

Range(s).Cells(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

【讨论】:

    猜你喜欢
    • 2019-03-15
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多