Two Formats of the IfThen Statement

The If...Then statement has two formats: a single line format and a multi-line format. The short format is good for statements that fit on one line, like:

If secretCode <> "01W01" Then MsgBox "Access denied" or

If secretCode = "01W01" Then alpha = True : beta = False

In the above examples, secretCode, alpha, and beta are the names of variables. In the first example, Visual Basic displays the message "Access denied" if the value of the secretCode variable is not equal to 01W01. In the second example, Visual Basic will set the value of the variable alpha to True and the value of variable beta to False when the secretCode value is equal to 01W01. Notice that the second statement to be executed is separated from the first one by a colon. The multi-line If...Then statement is more clear when there are more statements to be executed when the condition is true, or when the statement to be executed is extremely long.

Part I

0 0

Post a comment