Variables in Conditional Formatting Formula1

后端 未结 1 1882
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 06:30

I\'m trying to use variables in the FormatCondition Formula1 property. The variables will be cell references. However, I can\'t get the syntax right. The two bits I\'m having t

相关标签:
1条回答
  • 2021-01-26 06:48

    You just need to dynamically set your ranges by getting last row and column of your data where you can find many examples here like this one. Something like:

    Dim r As Range
    Dim lr As Long, lc As Long
    Dim formula As String
    
    With Sheet1 '~~> change to your actual sheet
        lr = .Range("C" & .Rows.Count).End(xlUp).Row '~~> based on C, adjust to suit
        lc = .Cells(3, .Columns.Count).End(xlToLeft).Column '~~> based on row 3
        Set r = .Range(.Cells(3, 3), .Cells(lr, lc))
        formula = "=(" & r.Address & "=""CM"")"
        '~~> formatting code here
    End With
    

    Or you can try what I've posted here about Conditional Formatting which of course can be automated as I posted HERE and HERE. Something like:

    formula = "=C3=""CM"""
    [C3].FormatConditions.Add xlExpression, , formula
    With [C3].FormatConditions(1)
        .Interior.Color = RGB(196, 189, 151)
        .ModifyAppliesToRange r
    End With
    

    HTH.

    0 讨论(0)
提交回复
热议问题