'How to combine or and and in the if statement vba in with do until

Is it possible to combine Do Until with If statement containing Or and And?

This is not working correctly, it doesn't take AND condition into consideration, it runs but like AND wouldn't be there

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

And when I try it to do with And and Or combination with brackets, it's not working at all

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

Is there any possibility how to combine it? What I am trying to achieve is, when one cell is containing "Role Adjustment" or "New Role" and other cell is containing "FALSE", to change the color of the cell.



Solution 1:[1]

Your second option should work, and @Grade'Eh'Bacon is probably correct:

Change this:

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

to this:

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

or

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

Test code:

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

Solution 2:[2]

It is a simple case of realizing how excel reads the IF statement. It is evaluating the Or statement first and going True and never looking at the And It can get a little ugly but you have to use the And first and then Or it. Try this

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

This evaluates the And condition first and if False then looks at the Or.

Conditional Formatting way.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paul bica
Solution 2