VBA Training03

download VBA Training03

of 22

Transcript of VBA Training03

  • 8/14/2019 VBA Training03

    1/22

    Visual Basic for ApplicationBasic Excel Macro (VBA) Orientation Course

  • 8/14/2019 VBA Training03

    2/22

    Loading

  • 8/14/2019 VBA Training03

    3/22

    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

  • 8/14/2019 VBA Training03

    4/22

    Getting Started with VBA Part 2:

    Basic Programming Fundamentals

    Conditionals & Branching

    Loops

    Arrays

    Simple Debug & Error Handling

    Second Program Assignment

    Recap

    http://recap/VBA%20Training03_p1.AVIhttp://recap/VBA%20Training03_p1.AVI
  • 8/14/2019 VBA Training03

    5/22

    Conditionals & Branching

    -uses conditional statements to change the flow or direction of program execution

    -implement decisions in VBA

    Getting Started with VBA Part 2:

    If Statements Select/Case Statements

    If (condition) Then

    Block of Code Statements

    End if

    If/Then Statement

    -one way selection

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    6/22

    Conditionals & Branching

    Getting Started with VBA Part 2:

    If Statements

    If/Then/Else Statement

    -two way selection

    If (condition) Then

    Block of Code StatementsElse

    Block of Code Statements

    End if

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    7/22

    Conditionals & Branching

    Getting Started with VBA Part 2:

    If Statements

    If/Then/ElseIfElse Statement

    -multi way selection

    If (condition) Then

    Block of Code StatementsElseif

    Block of Code Statements

    Else

    Block of Code Statements

    End if

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    8/22

    Conditionals & Branching

    Getting Started with VBA Part 2:

    Select/Case Statements

    -more if statements; difficult to follow the logic of the program

    Select Case Expression

    Case condition1

    Block of Code Statements

    Case condition2

    Block of Code Statements

    Case Else

    Block of Code Statements

    End Select

    Note:

    -multichoice conditions are easier to understand

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    9/22

    Getting Started with VBA Part 2:

    -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# 5

    -Make a program the will ask the user to input Time IN

    -determine the shiftcodes

    -If or Select/Case and Loops

    Assignment# 6

  • 8/14/2019 VBA Training03

    10/22

    Getting Started with VBA Part 2:

    -repetition of a block of code for a specified number of times

    Loops

    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

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    11/22

    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

    Block of codes

    Loop Until (Condition)

    Do Loop Until

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

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    12/22

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

    Do Until (Condition)

    Block of codes

    Loop

    Do Until 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

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    13/22

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

    Do While (Condition)

    Block of codes

    Loop

    Do While Loop

    -execute codes only if condition is True

    Exit For

    Exit Do

    -provides a way to exit a For loop

    Exiting Loop

    -provides a way to exit a Do loop

    Example

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    14/22

    Getting Started with VBA Part 2:

    -variable that can hold multiple values

    -related sets of values to be stored in a variable

    -single variable with many individual compartments

    Arrays

    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 IntegerExample

    http://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xlshttp://c/RalphDocs/rmamac/MIne/files/VBA%20Training/Passing.xls
  • 8/14/2019 VBA Training03

    15/22

    Getting Started with VBA Part 2:

    Arrays

    One-Dimensional Arrays

    Dim Student (9) as String

    0

    1

    32

    5

    6

    4

    7

    8

    9

    Dim Student (1 TO 10) as Integer

    1

    3

    2

    5

    6

    4

    7

    8

    910

  • 8/14/2019 VBA Training03

    16/22

    Getting Started with VBA Part 2:Arrays

    Dim Student (1,6) as Integer

    Multi-Dimensional Arrays

    0

    1

    32 64 50 1

    G tti St t d ith VBA P t 2

  • 8/14/2019 VBA Training03

    17/22

    Getting Started with VBA Part 2:

    Dynamic Arrays

    -size is specified in the declaration

    -cannot be changed while the program is running

    -static array

    Dim myArray (10) as Integer

    Fixed Length Arrays

    -length of an array is unknown

    -cannot be changed while the programmin is running

    Dim myArray ( ) as IntegerReDim myArray ( variable)

    ReDim Preserve myArray ( variable)

    Arrays

    G tti St t d ith VBA P t 2

  • 8/14/2019 VBA Training03

    18/22

    Getting Started with VBA Part 2:

    -result ofgrammatical, punctuation or spelling mistakes

    Types of Programming Errors

    Errors & Debugging

    -Errors (bugs)-fixing errors

    Syntax Errors

    Run-time Errors

    -errors occur during program execution

    Logical Errors

    -program doing something different to that of its intended meaning

    G tti St t d ith VBA P t 2

  • 8/14/2019 VBA Training03

    19/22

    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

    G tti St t d ith VBA P t 2

  • 8/14/2019 VBA Training03

    20/22

    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

    G tti St t d ith VBA P t 2

  • 8/14/2019 VBA Training03

    21/22

    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 users password. The user is onlyallowed 3 tries to enter the password, or the program terminate the

    application/or end the program.

  • 8/14/2019 VBA Training03

    22/22

    Chapter 3

    Chapter4