如何使用VBA获取Excel中用于条件格式的图标集的type/ID?

How to get the type/ID of the icon set used for Conditional Formatting in Excel using VBA?

我是 Excel 和 VBA 的新手。我有一个工作表,其中我使用图标集将条件格式应用于一系列单元格。我需要获取图标集的 ID,以便我可以使用 VBA.
[= 检查工作表中是否使用了特定的图标集25=] 我能够使用以下方法更改范围 E2:E10 的图标集:

Dim rg As Range
Dim iset As IconSetCondition
Set rg = Range("E2:E10")
rg.FormatConditions.Delete
Set iset = rg.FormatConditions.AddIconSetCondition
iset.IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1)


我能够使用以下方法检索图标集的整数 ID:

iconID = iset.IconSet.ID
' This returned 4, which corresponds to xl3TrafficLights1


我需要的是能够检索已应用于一系列单元格的图标集的 ID,以便我可以将其与特定 ID 进行比较。

像这样:

Dim rg As Range
Set rg = Range("E2:E10")

iconID = rg.IconSet.ID ' ==> This is what I need

If iconID = 4 Then
    MsgBox ("Success")
Else
    MsgBox ("Failed")

请帮忙。谢谢。

我不知道为什么,但下面的代码显示 return 您想要的号码。

Dim Rng As Range
Dim iCon As IconSetCondition

Set Rng = Cells(2, 1)
Set iCon = Rng.FormatConditions(1)
Debug.Print iCon.IconSet.ID

请注意,如果 Rng 没有设置 CF [涉及图标集],它将失败。