If Then Else is a decision making statement based on the given condition. It first checks the condition, if the condition is true then it executes the set of statements for the true, if the condition is false the it executes the set of statements for false. It is simplest form to interrupt the flow of program.
If condition Then
Statements
Else
Statement
Using the If?Then?Else statements require the use of comparison operators to build the needed condition statements. The comparison operators compare two or more values and decide if the values are equal to each other or if one is greater than the other; then the operators return a True or False answer.
Select Case Statements
Select Case statements allow you to check for multiple values at once.
Select [Case] test condition
Case expression list
Statements
[Case Else
Statements
End Select
Example
Select Case strMonth
Case "February"
IntDays = 28
Case "April", "June", "September", "November"
IntDays = 30
Case "January", "March", "May", "July", "August", "October", "December"
IntDays = 31
End Select
Loops
You might get some situation where you want to execute some set of statements several numbers of times. To do this we have several kinds of loops in VBA. Loops can be divided in two categories (1) Iteration loops like FOR loop (2) Logical loops like WHILE loop.
Iteration Loops
Iteration loops have a fixed starting and ending point. These loops are used to repeat the execution of set of statements specific number of times.
Dim I as integer
For I = 1 to 5
Msgbox ("Counting? & I)
Next I
Logical Loops
Logical loops have no predetermined number of iterations. Instead, they rely on a logical expression that tests for a particular condition and then repeat the loop until the condition is either met or cleared, depending upon the type of loop. Based on the result of the condition logical loops executes the code.
They can be simplified into two styles: those that test the condition before performing an action and those that test the condition after performing an action.
The Do While?Loop and Do Until?Loop both test the condition before performing the action within the loop. The difference between the two is that Do While loops perform the action when the condition is true and Do Until loops perform the action while the condition is false.
Dim intCounter as Integer
IntCounter = 1
Do While IntCounter <= 10
IntCounter = intCounter + 1
Loop
Dim IntCounter as Integer
IntCounter = 1
Do Until IntCounter = 11
IntCounter = intCounter + 1
Loop
The Do?Loop While and Do?Loop Until loops are similar to each other, just as they are similar to the Do While and Do Until loops mentioned earlier. Both check the conditional expression at the end of the loop, guaranteeing at least one pass through the loop.
GoTo Statement
The GoTo statement forces the procedure to change immediately to another section of the procedure unconditionally. The section of code that is to be branched to needs to be marked with either a line number or a line label, allowing the program to jump either forward or backward within the procedure. The best use GoTo statement is in error handling, you can direct the control of program to the error handler based on the error code generated while the execution of program.
The syntax for the GoTo statement is shown here:
GoTo line
Because GoTo statements can lead to unstructured code, using them is strongly discouraged. If used sparingly, they can be very useful.
Shailendra Singh has sinced written about articles on various topics from Software, Search Engines and Software. Website DirectoryWe are the fastest growing human edited web directory in the world. Listing of new website in 48 hours on static html pages with static text links.We. Shailendra Singh's top article generates over 12100 views. Bookmark Shailendra Singh to your Favourites.
Business Properties For Lease 5.Has a story of making loans for the specified type of property under condition.6.Can suit both small and large commercial loans for remarkable purpose commercial properties