简体   繁体   English

使用OR函数的多个IF语句条件不起作用-VBA

[英]multiple IF statement conditions using OR function doesn't work - VBA

I'm trying to get the code to check if multiple conditions are true and if so, to then change the colour of the cell but this doesn't work and highlights way more than necessary. 我正在尝试获取代码来检查是否满足多个条件,如果是,则更改单元格的颜色,但这不起作用,并突出显示了不必要的方式。 I'm not sure why the code doesn't work as intended: 我不确定为什么代码无法按预期工作:

Sub ConditionCheck()

Dim wb As Workbook
Dim ws As Worksheet
Dim lastrow As Long
Dim rng1 As Range
Dim rng2 As Range
Dim c As Range
Dim d As Range

Set wb = ActiveWorkbook
Set ws = wb.Sheets("Source")
lastrow = Sheets("Source").Cells(Rows.Count, "A").End(xlUp).Row

i = 2

For i = 2 To lastrow

    If Cells(i, 8) = "Cancelled Not Applicable" Or Cells(i, 8) = "Completed" Or Cells(i, 8) <> "" Then
        If Cells(i, 23) <> "Cancelled" Or Cells(i, 23) <> "Completed" Then
            Cells(i, 23).Interior.ColorIndex = 4
        End If
    End If
Next i


End Sub

Sample Data: 样本数据:

CASE ID       CASE STATUS                PROGRAM STATUS
10001         Active                     Pending
10002         Completed                  Pending Review
10004         Cancelled Not Applicable   Cancelled
If Cells(i, 8) = "Cancelled Not Applicable" Or Cells(i, 8) = "Completed" Or Cells(i, 8) <> "" Then
    If Cells(i, 23) <> "Cancelled" Or Cells(i, 23) <> "Completed" Then
        Cells(i, 23).Interior.ColorIndex = 4
    End If
End If

This says: 这说:

If Your case status is "Cancelled Not Applicable" or you case status is "Completed" or your case status is any value (not empty) then go ahead and test if your program status is any value (as it can't be both "Cancelled" AND "Completed" at the same time) then go ahead and highlight it. 如果您的案例状态为“已取消不适用”,或者案例状态为“已完成”,或者案例状态为任何值(非空),请继续测试程序状态是否为任何值(因为不能同时为“已取消”和“已完成”),然后突出显示它。

So really there are two issues here. 所以这里确实有两个问题。

  1. Your Or Cells(i, 8).value <> "" Here you are saying "Pass this IF line as true if this cell is blank" because it's an OR . Your Or Cells(i, 8).value <> ""在这里,您说的是“如果此单元格为空,则将此IF行传递为true”,因为它是OR It's like saying "If this chicken is red or this chicken is black or this chicken exists" So if you are testing a blue chicken it will pass as it exists. 这就像说:“如果这只鸡是红色的,或者这只鸡是黑色的,或者这只鸡存在”,那么,如果您正在测试一只蓝色的鸡,它将通过它的存在。

  2. Your Cells(i, 23) <> "Cancelled" Or Cells(i, 23) <> "Completed" . 您的Cells(i, 23) <> "Cancelled" Or Cells(i, 23) <> "Completed" If the cell contains "Cancelled" then your second condition is true since it doesn't say "Completed" so this passes. 如果单元格包含“已取消”,则您的第二个条件为true,因为它没有显示“已完成”,因此可以通过。 If your cell says "Completed" then your first condition passes as it's not "Cancelled". 如果您的单元格显示“已完成”,则您的第一个条件通过,因为它不是“已取消”。 If your cell says "Booger farts" then both conditions pass as it's not "Cancelled" nor is it "Completed". 如果您的单元格显示“笨蛋放屁”,则两个条件都将通过,因为它既不是“已取消”,也不是“已完成”。 So really you want AND here. 所以真的想要AND这里。

That second point is difficult to grasp since "OR" isn't how we would use it english. 第二点很难理解,因为“ OR”不是我们用英语的方式。 It may help to think of it this way: If we have a test like IF condition1 OR condition2 OR condition3 then only one of those conditions needs to be true for this to pass. 这样考虑可能会有所帮助:如果我们有一个类似IF condition1 OR condition2 OR condition3的测试,则只有其中一个条件为真才能通过。 If TRUE OR FALSE OR FALSE passes. If TRUE OR FALSE OR FALSE通过。 Your conditions here are "Not equal to" which adds to the confusion, but you merely have to concentrate on the individual condition and determine if it returns "TRUE" or "FALSE" and make your statement IF TRUE OR FALSE (passes) or IF TRUE OR TRUE (passes) or IF FALSE OR TRUE (passes) or IF FALSE OR FALSE (finally it fails!). 您在这里的条件是“不等于”,这增加了混乱,但是您只需要专注于单个条件并确定它是否返回“ TRUE”或“ FALSE”,并让您的语句为IF TRUE OR FALSE (通过)或IF TRUE OR TRUE (通过)或IF FALSE OR TRUE (通过)或IF FALSE OR FALSE (最终失败!)。

Instead you wish to say: 相反,您想说:

If the CASE STATUS is filled in and if the PROGRAM STATUS is IN PROGRESS or PENDING or PENDING REVIEW, etc. then the CASE STATUS cannot be CANCELLED OR COMPLETED 如果填写了“案例状态”,并且“程序状态”为“正在进行中”,“正在等待审核”或“正在等待审核”等,则无法取消或完成“案例状态”

So: 所以:

If Cells(i,8).value <> "" AND (Cells(i, 8).value = "Cancelled Not Applicable" OR Cells(i,8).value = "Completed") AND (Cells(i, 23).Value <> "Cancelled" AND Cells(i, 23).Value <> "Completed") Then
    Cells(i, 23).Interior.ColorIndex = 4
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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