excel 如何在if语句中合并or和and vba in with do until

5n0oy7gb  于 2023-01-31  发布在  其他
关注(0)|答案(2)|浏览(162)

是否可以将Do Until与包含OrAndIf语句组合在一起?
这不能正常工作,它没有考虑AND条件,它运行但AND不存在

i = 2
Do Until IsEmpty(Cells(i, 1))
    If Cells(i, 13) = "Role Adjustment" Or Cells(i, 13) = "New Role" _
            And Cells(i, 15) = "FALSE" Then
        Cells(i, 15).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 49407
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
    i = i + 1
Loop

当我尝试使用AndOr与括号的组合时,根本不起作用

i = 2
Do Until IsEmpty(Cells(i, 1))
    If (Cells(i, 13) = "Role Adjustment" Or Cells(i, 13) = "New Role") _
            And Cells(i, 15) = "FALSE" Then
        Cells(i, 15).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 49407
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
    i = i + 1
Loop

有没有可能把它们组合起来?我想实现的是,当一个单元格包含"Role Adjustment""New Role",另一个单元格包含"FALSE"时,改变单元格的颜色。

dm7nw8vv

dm7nw8vv1#

你的第二个选择应该会起作用,@Grade 'Eh' Bacon可能是正确的:
更改此内容:

If Cells(i, 13) = "Role Adjustment" Or Cells(i, 13) = "New Role" _
        And Cells(i, 15) = "FALSE" Then

改为:

If (UCase(Cells(i, 13).Value2) = "ROLE ADJUSTMENT" _
    Or _
    UCase(Cells(i, 13).Value2) = "NEW ROLE") _
    And _
    Cells(i, 15).Value2 = False Then

If (UCase(Cells(i, 13).Value2) = "ROLE ADJUSTMENT" _
    Or _
    UCase(Cells(i, 13).Value2) = "NEW ROLE") _
    And _
    UCase(Cells(i, 15).Text) = "FALSE" Then

试验代码:

Sub testLogicalOps()
    Dim y As Range
    For Each y In Range("B1:B10")
        Debug.Print IIf((y = 5 Or y = 3) And y.Offset(0, 1) = False, "Yes", "No")
    Next
End Sub
9jyewag0

9jyewag02#

这是一个实现excel如何读取IF语句的简单例子。它首先计算Or语句,然后返回True,而从不查看And。可能会有点难看,但您必须首先使用And,然后再使用Or。尝试以下操作

If Cells(i, 13) = "Role Adjustment" And Cells(i, 15) = "FALSE" Or Cells(i, 13) = "New Role" And Cells(i, 15) = "FALSE" Then

这将首先计算And条件,如果为False,则查看Or。
条件格式设置方式。

=AND($O1="FALSE", OR($M1="Role Adjustment", $M1="New Role"))

相关问题