IF judgment statement and comparison operator in Excel VBA

Among the branch structures of VBA, the most commonly used one is the IF...Then...Else structure, whose syntax is

(single line form)

If condition Then [statements] [Else elsestatements]

or (for line breaks, you need to add END IF at the end)

If condition 
   [statements]
End If

or (with the else branch)

If condition Then
    [statements]
[ElseIf condition-n Then
    [elseifstatements]]
[Else
    [elsestatements]]
End If

 

Among them, condition is a logical judgment expression; Else I f are connected together without spaces in between.

The first form is a single-line form. There is only one expression after the Then keyword, and Else and End If can be omitted. It is recommended that when there is only one simple judgment, one judgment is enough.

But if it is a little more complicated, the statement after Then has a task to perform, so honestly use the following multi-line form.

The comparison operators supported by VBA are as follows.

Suppose variable A=10 and variable B=20, then:

operator

describe

example

=

Checks whether the values ​​of the two operands are equal. If yes, then the condition is true.

(A = B) The result is: False

<>

Checks whether the values ​​of the two operands are not equal. If the values ​​are not equal, the condition becomes true.

(A <> B) results in: True

>

Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true.

(A > B) the result is: False

<

Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true.

(A < B) results in: True

>=

Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true.

(A >= B) the result is: False

<=

Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true.

(A <= B) the result is: True

Guess you like

Origin blog.csdn.net/xijinno1/article/details/130144035
Recommended