简体   繁体   中英

How to perform multiple AND statement after THEN Statement

Sub Crons()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim check1 As Boolean, check5 As Boolean, check10 As Boolean, flow As Boolean, speed As Boolean, motor_power As Boolean
    
    Set s1 = ThisWorkbook.Worksheets(1)
    Set s2 = ThisWorkbook.Worksheets(2)
    
    check1 = s2.CHECKBOXES("Check Box 1").Value = xlOn
    check5 = s2.CHECKBOXES("Check Box 5").Value = xlOn
    check10 = s2.CHECKBOXES("Check Box 10").Value = xlOn
           
    flow = s1.Range("B26").Value = "Flow(from fill level)"
    speed = s1.Range("B24").Value = "Speed"
    motor_power = s1.Range("B25").Value = "Motor Power"
    
    If check1 Or _
        ((check10 Or check5) Or _
        (flow Or speed) Or _
        (motor_power)) Then
            s1.Range("B30").Value = "Activate anomaly detection" And _
            s1.Range("B31").Value = "Activate Operating hour" And _
            s1.Range("B32").Value = "Activate cycle time"
    Else
        s1.Range("B30").Value = vbNullString And _
        s1.Range("B31").Value = vbNullString And _
        s1.Range("B32").Value = vbNullString
    End If
    
End Sub

Just don't use AND !

If check1 Or _
    ((check10 Or check5) Or _
    (flow Or speed) Or _
    (motor_power)) Then
        s1.Range("B30").Value = "Activate anomaly detection" 
        s1.Range("B31").Value = "Activate Operating hour"
        s1.Range("B32").Value = "Activate cycle time"
Else
    s1.Range("B30").Value = vbNullString 
    s1.Range("B31").Value = vbNullString 
    s1.Range("B32").Value = vbNullString
End If

You misunderstand the And operator. It has two purposes:

  • A logical operator, similar to the Or you've used in your If statement. Whereas True Or False equals True , True And False equals False .
  • A bitwise operator, taking two integer operands and returning a value consisting of the 1 bits present in both operands. For example, 14 And 7 = binary 1110 And 0111 = binary 0110 = 6.

Trying to use it as a procedural statement ("do X, and then do Y") per your code is not only incorrect but makes no sense, since this is the ordinary behaviour of two statements in sequence.

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