VBA 遇到条件格式问题 excel

VBA having trouble with conditional formatting excel

Sub InsertingIcons()
'
' What's wrong with macro. I want macro to run from Cell F12 to H 12 and change to conditional fomatting
'

'for loop
Dim x As Variant

For x = 7 To 11

    Selection.FormatConditions.AddIconSetCondition
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1)
        .ReverseOrder = False
        .ShowIconOnly = False
        .IconSet = ActiveWorkbook.IconSets(xl3Arrows)
    End With
    With Selection.FormatConditions(1).IconCriteria(2)
        .Type = xlConditionValueNumber
        .Value = "='DETAILED BS'!" & R12 & x
        .Operator = 7
    End With
    With Selection.FormatConditions(1).IconCriteria(3)
        .Type = xlConditionValueNumber
        .Value = "='DETAILED BS'!" & R12 & x
        .Operator = 5
    End With
    
    Next x
    End
End Sub

试试这个:

Sub Conditional_IconSets()
Dim Rng As Range
Dim Sh As Worksheet

Set Sh = Worksheets("DETAILED BS")

Set Rng = Range("F12:H12")
Rng.FormatConditions.Delete

Rng.FormatConditions.AddIconSetCondition
    With Rng.FormatConditions(1)
        .ReverseOrder = False
        .ShowIconOnly = False
        .IconSet = ActiveWorkbook.IconSets(xl3Arrows)
    End With
    With Rng.FormatConditions(1).IconCriteria(2)
        .Type = xlConditionValueNumber
        .Value = Sh.Range("R12")
        .Operator = 7
    End With
    With Rng.FormatConditions(1).IconCriteria(3)
        .Type = xlConditionValueNumber
        .Value = Sh.Range("R12")
        .Operator = 5
    End With
End Sub