Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CONTROLLING

PROGRAM
FLOW
ผศ.ดร. นาถฤดี ศุภกิจจาร ักษ ์
• Create Comparisons
• Make Use of Logical Operators
• Employ Do While Loops
• Create Do Until Loops
• Create For Next Loops
• Create If Then Else Statements
• Construct Select Case Statements
• GoTo a Named Location
• Call a Procedure

What you will learn??


• Return “True” or “False”

CREATE COMPARISON
• Execute VBA or a series of VBA • We can also use the
statements as long as a condition
is True by using Do While loop. following syntax to create a
Do-Loop
Do [While condition]
[Statement]
Do
Loop [Statement]
• A condition is an expression that Loop [While condition]
evaluates to either True or False. If
the condition is True, it executes
the statement. If the conditions is
False, VBA executes the statement
after the Loop statement

Employ Do While Loops


1. New procedure
2. Declare variables
3. Assign Values to variable
4. Use a Do While Statement to evaluate whether a condition is true
5. Place the value of J in the specified cell
6. Increase the value of the counter
7. Add the Loop ‘Statement

Exercise Do While Loop Result


Exercise
1. New procedure
2. Declare Variable
3. Assign a Value to variable
4. Add a Do Until Statement
5. Type the statements you want to execute
– multiplies the value in column A by
0.07 and places the result in column B
6. Add the Loop statement

Do Until Loops
Exercise
1. New procedure
2. Declare variable
3. Add a For statement
1. Counter variable
2. Initial value
3. Maximum value
4. [Statement] to execute
5. Add a Next statement

For Next Loops


Exercise
1. New procedure
2. Declare array
3. Declare variables
4. Assign values to array e.g. value in the
active cell and three subsequent cells in
the same column are assigned to the
array
5. Add For Each In statement
6. Add Next statement
7. Type any executed statement when For
Each In loop complete

For Each in Loops


Exercise HOMEWORK

Write the code to creates a two-dimensional array


with the value shown above
Exercise
1. New procedure
2. Declare variable
3. Add If Then statement
1. Condition
2. Statement to execute
4. Add Else statement
5. Type End If If condition Then

[Statement] If the sales column is over


Else
50,000, calculates a bonus of
4 percent of sales, otherwise,
If then Else statement [Statement] print the words “No Bonus”
End If
Exercise
1. Add If Then statement condition
1. Statement to execute
2. Add Elseif statement
1. Statement to execute
3. Add Else statement
1. Statement to execute
4. Type End if statement

Tax
TX 5%, FL 8%,CA 10%, else No tax
Exercise
1. New procedure
2. Declare variable
Select Case testexpression
3. Initialize variable [caseexpressionlist –n
4. Create a Do While loop [statements-n]

5. Type Select Case statement [Case Else

[Eesetatement]
6. Type Case statement
End Select
7. Add Case Else statement
8. Add End Select statement

Select Case statement


Exercise: Calculates a shipping charge if
cost with tax is less than $50,000
1. New procedure
2. Add code
3. Add GoTo Statement
4. Add GoTo
1. label: Label names are followed by a colon

5. Add any additional code

GoTo a Named Location


Example
1. Name a procedure
2. Declare and initialize
any variables
1. May need to make
variable public
3. Create an If then
condition
4. Call another procedure

Call a Procedure: if you are in one procedure and want to


execute another procedure. Type “Call” followed by name of
procedure

You might also like