Vous êtes sur la page 1sur 22

Visual Basic for Application

Basic Excel Macro (VBA) Orientation Course


Loading…
Course Objectives:

To have a basic knowledge on VBA


To be able to learn the VBA Programming Fundamentals
To be able to make VBA programs
Getting Started with VBA Part 2:
Basic Programming Fundamentals
Conditionals & Branching Recap

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 Statements Select/Case Statements

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

For Loops Do Loops

-number of iteration for the loop is known


-definite iterations

For variable = start To end Step value


‘Block of Codes
Next variable

Example
Getting Started with VBA Part 2:
Loops
Do Loops

-number of iteration for the loop is unknown


-indefinite iterations
-loop will execute as many times as needed until the condition is meet

Do Loop Until

Do
‘ Block of codes
Loop Until (Condition)

-execute codes at least one and continues to loop if condition is false

Example
Getting Started with VBA Part 2:
Loops>>Do Loops

Do Until Loop

Do Until (Condition)
‘ Block of codes
Loop

-execute codes only is the condition is false

Do Loop While

Do
‘ Block of codes
Loop While (Condition)

-execute codes at least one and continues to loop if condition is True


Example
Getting Started with VBA Part 2:
Loops>>Do Loops

Do While Loop

Do While (Condition)
‘ Block of codes
Loop

-execute codes only if condition is True


Exiting Loop

Exit For

-provides a way to exit a For loop

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

Dim myArray (number of elements) as Type

Dim myArray (10) as Integer

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

Dim Student (9) as String Dim Student (1 TO 10) as Integer

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

Fixed Length Arrays

-size is specified in the declaration


-cannot be changed while the program is running
-static array
Dim myArray (10) as Integer

Dynamic Arrays

-length of an array is unknown


-cannot be changed while the programmin is running
Dim myArray ( ) as Integer
ReDim myArray ( variable)
ReDim Preserve myArray ( variable)
Getting Started with VBA Part 2:
Errors & Debugging
-Errors (bugs)
-fixing errors

Types of Programming Errors

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

-must be followed with instructions to VBA for deciding a course of action


when a runtime error is encountered

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

Vous aimerez peut-être aussi