Excel VBA选择最后一行的多列并应用条件格式

mspsb9vt  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(177)

有人可以帮助我在我下面的查询。我试图选择多列与最后一行和应用条件格式,但它不是在我尝试的方式工作。
工作一:

Dim lr As Long

With Range("E:E,F:F,H:H")
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).Interior.Color = 13561798
    .FormatConditions(1).StopIfTrue = False
End With

这一个不起作用:

Dim lr As Long

    lr = Cells(Rows.Count, 1).End(xlUp).Row

With Range(""E2:E" & lr,"F2:F" & lr,"H2:H" & lr)
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).Interior.Color = 13561798
    .FormatConditions(1).StopIfTrue = False
End With

我尝试对包含错误“#N/A”的单元格应用颜色,但它不起作用。

Dim lr As Long

    lr = Cells(Rows.Count, 1).End(xlUp).Row

With Range(""E2:E" & lr,"F2:F" & lr,"H2:H" & lr)
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=#N/A"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    .FormatConditions(1).Interior.Color = 13561798
    .FormatConditions(1).StopIfTrue = False
End With

任何帮助一个以上2查询将不胜感激。

baubqpgj

baubqpgj1#

试试这个:

Sub Test()
    Dim lr As Long, ws As Worksheet
    
    Set ws = ActiveSheet 'for example
    lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    With ws.Range("E2:E" & lr & ",F2:F" & lr & ",H2:H" & lr)
        With .FormatConditions.Add(Type:=xlCellValue, _
                                   Operator:=xlEqual, Formula1:="=0")
            .SetFirstPriority
            .Interior.Color = 13561798
            .StopIfTrue = False
        End With
    End With
End Sub

FormatConditions.Add返回刚刚添加的条件,因此您可以在With块中使用它。还修复了您的Range()调用。
针对#N/A

With .FormatConditions.Add(Type:=xlExpression, Formula1:="=isNA(E2)")

相关问题