【问题标题】:VBA having trouble with conditional formatting excelVBA 在条件格式方面遇到问题 excel
【发布时间】:2021-07-24 13:26:04
【问题描述】:
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

【问题讨论】:

    标签: vba for-loop next


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 那么,你只添加了 Set ?
    • 给你带来麻烦的那一行是 Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 和 set 你可以避免错误...没有什么其他...它对你有用吗?
    • 但我放 x var 是为了能够随时更改范围,也用于放置图标。在这种情况下它不比较,所以只放向上的图标
    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2015-11-05
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2020-04-13
    • 2016-10-02
    相关资源
    最近更新 更多