"VBA Programming" 06. Control statements

Source: Internet
Author: User
Tags case statement

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.