The use of if and ElseIf in VB is:
If...then...elseif...then...else...endif
Remember to do not add a colon after then, add a colon to the else without an if error, because the addition of a colon means that if the statement is only executed then, even if the execution is finished, so the following
1 SubTest ()2 DimI as Integer3 DimJ as Integer4 Dima5 fori =1 to 2:6Debug.Print ("This is the first"&Str(i) &"Secondary Cycle")7A =InputBox("Please enter")8 9 IfA ="1" ThenTen MsgBox("perform 1") OneDebug.Print ("perform 1") A ElseIfA =" -" Then - MsgBox("Perform 2") -Debug.Print ("Perform 2") the ElseIfA =" +" Then - MsgBox("Perform 3") -Debug.Print ("Perform 3") - ElseIfA =" -" Then + MsgBox("Perform 4") -Debug.Print ("Perform 4") + ElseIfA =" -" Then A MsgBox("Perform 5") atDebug.Print ("Perform 5") - Else: - MsgBox("Nothing has been done .") -Debug.Print ("Nothing has been done .") - End If - NextI in End Sub
If you add a colon, you can only be an if:
1 SubTest ()2 DimI as Integer3 DimJ as Integer4 Dima5 fori =1 to 2:6Debug.Print ("This is the first"&Str(i) &"Secondary Cycle")7A =InputBox("Please enter")8 9 IfA ="1" Then:Ten MsgBox("perform 1") OneDebug.Print ("perform 1") A - NextI - End Sub
If this is the following, you will get an error:
1 SubTest ()2 DimI as Integer3 DimJ as Integer4 Dima5 fori =1 to 2:6Debug.Print ("This is the first"&Str(i) &"Secondary Cycle")7A =InputBox("Please enter")8 9 IfA ="1" Then:Ten MsgBox("perform 1") OneDebug.Print ("perform 1") A ElseIfA ="2" Then: - MsgBox("Perform 2") - NextI the End Sub
vb if and ElseIf