简体   繁体   中英

Complicated IF condition including logical AND / OR

It runs but it does not do what it is supposed to do. What I need:

If cell B contains "RR", and if cell C does not equal "memo" or "correction" and cell G is not "air" or "printed" then change cell L to 0 .

If cell B contains "RR", and if cell C does not equal "memo" or "correction" and cell G is "air" or "printed" then change cell L to H*.1 .

Sub RRCLEAN()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim myString As String
With ActiveSheet
RowCount = WorksheetFunction.CountA(range("A:A"))

For i = 2 To RowCount
    myString = Trim(Cells(i, 2).Value)
    If InStr(myString, "RR") > 0 And .Cells(i, 3).Value <> "Memo" And .Cells(i, 3).Value <> "Correction" And .Cells(i, 7).Value <> "Air" Or .Cells(i, 7).Value <> "Printed" Then
        Cells(i, 12).Value = 0
    End If
Next

For i = 2 To RowCount
    myString = Trim(Cells(i, 2).Value)
    If InStr(myString, "RR") > 0 And .Cells(i, 3).Value <> "Memo" And .Cells(i, 3).Value <> "Correction" And .Cells(i, 7).Value = "Air" Or .Cells(i, 7).Value = "Printed" Then
        Cells(i, 12).Value = Cells(i, 8).Value * 0.1
    End If
Next
End With

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Use parentheses when needed. These are your main 2 corrected if conditions:

If InStr(myString, "RR") > 0 And .Cells(i, 3).Value <> "Memo" And .Cells(i, 3).Value <> "Correction" And .Cells(i, 7).Value <> "Air" And .Cells(i, 7).Value <> "Printed" Then

If InStr(myString, "RR") > 0 And .Cells(i, 3).Value <> "Memo" And .Cells(i, 3).Value <> "Correction" And (.Cells(i, 7).Value = "Air" Or .Cells(i, 7).Value = "Printed") Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM