"IF ... Then ... Statement
If condition Then
[Statements1]
Else
[Statements2]
End If
Condition is a logical expression that indicates the condition to be judged when making a selection, the result of which is a Boolean type, and when its value is true, executes the STATEMENTS1 statement, or false yes executes the STATEMENTS2 statement in else
"Code Area"
Private Subtest if ()DimAge as Integer Age=InputBox("Please enter your age","Enter Age window") If(Age >= -) Then MsgBox "already adult", vbOKOnly,"Judging Results" Else MsgBox "Minors", vbOKOnly,"Judging Results" End IfEnd Sub
"If nested statement"
IF condition Then
[Statements1]
[Elseif Condition2 then statements2]
[Elseif Condition3 then STATEMENTS3]
...
[Elseif Conditionn then STATEMENTSN]
Else statements
End if
"Code Area"
Private Subtest if nesting ()DimAge as Integer Age=InputBox("Please enter your age","Enter Age window") If(Age <=6) Then MsgBox "Childhood", vbOKOnly,"Judging Results" ElseIf(6< age andAge <= -) Then MsgBox "Juvenile", vbOKOnly,"Judging Results" ElseIf( -< age andAge <= +) Then MsgBox "Youth", vbOKOnly,"Judging Results" ElseIf( A< age andAge <= $) Then MsgBox "aged", vbOKOnly,"Judging Results" Else MsgBox "old age", vbOKOnly,"Judging Results" End IfEnd Sub
"Select Case Statement"
Select Case testexpression
[Case Expressionlist-n]
[Statements-n] ...
[Case Else
[Elsestatements]]
End Select
"Code Area"
Private SubCase Test ()DimAge as Integer Age=InputBox("Please enter your age","Enter Age window")Select Case Age Case 0 to 6 MsgBox "Childhood", vbOKOnly,"Judging Results" Case 6 to - MsgBox "Juvenile", vbOKOnly,"Judging Results" Case - to + MsgBox "Youth", vbOKOnly,"Judging Results" Case + to $ MsgBox "aged", vbOKOnly,"Judging Results" Case Else End SelectEnd Sub
"Tips"
In Select Case, you need to be aware of the upper and lower bounds, for example: "Case 0 to 6" means 6>=age>0
"VBA Programming" 06. Control statements