Академический Документы
Профессиональный Документы
Культура Документы
Loops
Arrays
Simple Debug & Error Handling
Second Program Assignment
Getting Started with VBA Part 2:
Conditionals & Branching
-uses conditional statements to change the flow or direction of program execution
-implement decisions in VBA
If/Then Statement
-one way selection
If (condition) Then
‘Block of Code Statements
End if
Example
Getting Started with VBA Part 2:
Conditionals & Branching
If Statements
If/Then/Else Statement
-two way selection
If (condition) Then
‘Block of Code Statements
Else
‘Block of Code Statements
End if
Example
Getting Started with VBA Part 2:
Conditionals & Branching
If Statements
If/Then/ElseIf…Else Statement
-multi way selection
If (condition) Then
‘Block of Code Statements
Elseif
‘Block of Code Statements
Else
‘Block of Code Statements
End if
Example
Getting Started with VBA Part 2:
Conditionals & Branching
Note:
-more if statements; difficult to follow the logic of the program
Select/Case Statements
-multichoice conditions are easier to understand
Select Case Expression
Case condition1
‘Block of Code Statements
Case condition2
‘Block of Code Statements
Case Else
‘Block of Code Statements
End Select
Example
Getting Started with VBA Part 2:
Assignment# 5
-Make a program the will ask the user to input the Sex of the Person
-Male = output message
-Female=output message
-Other=output message/ask user to input again
1. If Statement
2. Select/Case Statement
3. Loops
Assignment# 6
-Make a program the will ask the user to input Time IN
-determine the shiftcodes
-If or Select/Case and Loops
Getting Started with VBA Part 2:
Loops
-repetition of a block of code for a specified number of times
Example
Getting Started with VBA Part 2:
Loops
Do Loops
Do Loop Until
Do
‘ Block of codes
Loop Until (Condition)
Example
Getting Started with VBA Part 2:
Loops>>Do Loops
Do Until Loop
Do Until (Condition)
‘ Block of codes
Loop
Do Loop While
Do
‘ Block of codes
Loop While (Condition)
Do While Loop
Do While (Condition)
‘ Block of codes
Loop
Exit For
Exit Do
-provides a way to exit a Do loop
Example
Getting Started with VBA Part 2:
Arrays
-variable that can hold multiple values
-related sets of values to be stored in a variable
-single variable with many individual compartments
One-Dimensional Arrays
-analogous to a single column in the spreadsheet
Multi-Dimensional Arrays
-analogous to a multiple column in the spreadsheet
Dim myArray (number of elements) as Type
Dim myArray (10,2) as Integer
Example
Getting Started with VBA Part 2:
Arrays
One-Dimensional Arrays
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8
9
9 10
Getting Started with VBA Part 2:
Arrays
Multi-Dimensional Arrays
Dim Student (1,6) as Integer
0 1 2 3 4 5 6
0
1
Getting Started with VBA Part 2:
Arrays
Dynamic Arrays
Syntax Errors
-result of grammatical, punctuation or spelling mistakes
Run-time Errors
-errors occur during program execution
Logical Errors
-program doing something different to that of its intended meaning
Getting Started with VBA Part 2:
Errors & Debugging
Debugging Tools
-Watch Window
-Immediate Window
-Local Window
-Step Out
-Step Over
-Step Into
-Toggle Breakpoint
-Reset
-Pause
-Play
Getting Started with VBA Part 2:
Errors & Debugging
Error Handling
On Error Statement
Resume Next
-sends the program execution to the next line of code
Getting Started with VBA Part 2:
Program Exercises
- Make a program that will ask the User to input 10 Scores, determine the highest
and the lowest
- Make a program that will ask the user to any Input from the user, determine if
the input starts with a letter, number or a special character
- Make a program that will ask for the users badge no., to output the users
complete name
- Make a program that that will ask for the users info, like name, age, address,
occupation, etc… then output the results.
- Make a program that that will ask for the user’s password. The user is only
allowed 3 tries to enter the password, or the program terminate the
application/or end the program.
Chapter 3
Chapter4