VBA Basics

Embed Size (px)

Citation preview

  • 8/12/2019 VBA Basics

    1/33

    9/15/13

    Dr. Antonio A. Trani

    VBA Principles

    CEE3804: Computer

    Applications for

    Civil and

    Environmental

    Engineers

  • 8/12/2019 VBA Basics

    2/33

    9/15/132

    Objectives

    ! Develop a simple set of programs in VBA! Learn to execute a macro from within a

    worksheet

    ! Introduction to loop structures

    CEE 3804

  • 8/12/2019 VBA Basics

    3/33

    9/15/13CEE 38043

    Before You Program

    ! Develop a strategy to solve the problem! Develop pseudo-code! Develop a flowchart (helps guide your thought process)

    ! NOTE: most engineers do not to like to develop flowcharts! This creates problems when someone else looks at your program! Most programs do not work the first time, so a flowchart of pseudo-

    code can help you understand bad logic

    Start

    Output

    c

    Inputb

    c = a + b

    Inputa

    End

    Start here:

    Flowchart Then code

  • 8/12/2019 VBA Basics

    4/33

    9/15/13CEE 38044

    Flowchart Symbols

    ! Review them in Chapras book (page 115)Symbol Name Function

    Terminal Represents the

    beginning of end of a

    programFlow lines Represent the flow of

    logic

    Process Calculation of data

    manipulation

    Input/Output Represents input and

    output of data andinformation

    Decision Represents a

    comparison or a

    condition with more

    than one path

    Start

    c = a + b

    Input

    b

    If (a>b) True

    False

  • 8/12/2019 VBA Basics

    5/33

    9/15/13CEE 3804 : A.A. Trani5

    Flowchart Symbols (cont.)

    Symbol Name Function

    Count-controlled

    loop

    Indicates the number

    of times a loop is

    excecutedJunction or

    connector

    Represents a

    confluence of flow

    lines

    Off-page connector Break or continuation

    to another page

    Stored data Represents datastorage in a database

  • 8/12/2019 VBA Basics

    6/33

    9/15/13CEE 38046

    A Simple VBA Program

    ! Lets develop a simple program in VBA tocalculate the sum of two numbers of a

    worksheet

    ! The program should retrieve two numbers fromsheet1in a standard Excel worksheet

    ! The program should add the two numbers andreturn the result to the worksheet

    !Place a run

    button in the worksheet tofacilitate future execution of the subroutine

  • 8/12/2019 VBA Basics

    7/33

    9/15/13CEE 38047

    Program Flowchart

    ! Here is a basic flowchart to build this programStart

    Outputc

    Inputb

    c = a + b

    Inputa

    End

    Start

    Outputc

    Input

    b

    c = a + b

    Input

    a

    End

    Version 1

    Version 2

  • 8/12/2019 VBA Basics

    8/33

    9/15/138

    Excel Worksheet (final program)

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    9/33

    9/15/13CEE 38049

    VBA or Macros in Old Excel Versions(including Mac 2011)

    ! Go to: Tools/Macro! Then select VBA

  • 8/12/2019 VBA Basics

    10/33

    9/15/1310

    VBA or Macros in Excel WIN 2013

    ! Go to : Developer Tab! Then Click Visual Basic

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    11/33

    9/15/1311

    VBA Code

    Name of subroutine

    Go to sheet1Select the value of cell B7

    and assign it to variable a

    Select the value of cell B8

    and assign it to variable

    bCalculate the value of c

    Select cell B8 and assign it

    to variable c

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    12/33

    9/15/1312

    Adding the RunButton

    ! Adding the runbutton requires that you inserta control button and then assign a macro to

    subroutine adder

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    13/33

    9/15/13CEE 380413

    Assigning the Behavior to theRunButton

    ! Assign the macro adderfrom the list ofavailable macros to button Run

    ! Right-click on the button to assign a behavior

  • 8/12/2019 VBA Basics

    14/33

    9/15/13CEE 380414

    Test Your Program

    ! Test the program to make sure it works

    ! Try variations of the program as well

  • 8/12/2019 VBA Basics

    15/33

    9/15/13CEE 380415

    Subroutine Adder1 with Input Box

    ! This variation of the program creates an inputbox for the first number

    String variable a

    is read using an

    input box

    Returns the

    number

    contained in the

    string a

  • 8/12/2019 VBA Basics

    16/33

    9/15/13CEE 380416

    Excel Help for VAL Function

  • 8/12/2019 VBA Basics

    17/33

    9/15/1317

    Executing the New Program

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    18/33

    9/15/13CEE 380418

    A Simple Program with a Loop

    ! Loops are natural ways to executecomputations that require multiple iterations

    ! Loops can be conditioned or controlled by acounter

    ! Conditional loops - when some condition isused to exit the loop

    !Counter controlled loops - when the number ofpasses in the loop is known

  • 8/12/2019 VBA Basics

    19/33

    9/15/13Copyright 200719

    First Program with a Loop

  • 8/12/2019 VBA Basics

    20/33

    9/15/1320

    The VBA Code Behind

    Loop code

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    21/33

    9/15/1321

    A Loop with Concatenation Control

    ! The program in worksheet: loopConcatenate.xlsoffers a sample of a loop computation and the

    use of concatenation control to estimate

    pavement thicknesses

    ! The pavement thickness function created inprevious classes in calledby the VBA code

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    22/33

    9/15/1322

    Worksheet Interface

    Cell B8 controls the

    number of times theloop is executed

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    23/33

    9/15/1323

    The Code Behind the Worksheet

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    24/33

    9/15/1324

    Code (cont.)

    Calculates pavement thickness

    Concatenation

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    25/33

    9/15/1325

    Try Other Refinements

    ! Currently the loop counter just overwrites thevalues of pavement thickness without erasing

    previous computation

    ! Try adding a line or two of code to erase theprevious table of computations while executingthe code

    Use a statement such as : Range(A1:C5).clear This clears the range between cells A1 and C5 You can always clear a large range of cells so that

    your program will always work correctly

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    26/33

    Simple Control Structure

    ! If-Then-Else is a powerful, yet simple controlstructure that could be used to make

    branching decisions in a program

    If (condition) then! Code 1 is executed

    Elseif (condition 2) then! Code 2 is executed

    Elseif (condition 3) then! Code 3 is executed

    End if!You can add as many else-if-then statements as

    needed in your program logic

    9/15/1326 CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    27/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! Suppose we would like to modify the previouslycreated program to replace the value of

    California Bearing Ratio (CBR) for the type of

    soil used in construction

    ! Suppose we have a simple table with typicalvalues of CBR as follows:

    9/15/1327

    Material Value of CBR (dim)

    Crush stone 100

    Sandy soil 25

    Silty soil 13

    Clay soil 7

    Organic soil 4

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    28/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! Recall the original spreadsheet

    9/15/1328

    Material Value of CBR (dim)

    Crush stone 100Sandy soil 25

    Silty soil 13

    Clay soil 7

    Organic soil 4

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    29/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! Create a list of items to be selected by the user incell B7

    ! Use the Data Validation in the Data Tab! Create a list in the validation criteria

    9/15/1329 CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    30/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! The list of items can be enumerated in anothersection of the spreadsheet

    ! In this example I used cells G7:G11 to write thenames of the types of soils to be selected

    9/15/1330 CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    31/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! Once the cell is connected with data validation onlythe names of the soils types can be selected in that

    cell

    9/15/1331 CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    32/33

    Modification of Pavement Analysis Program(Loop with Concatenation)

    ! Complete the VBA code to assign the value of CBRfor a selected soil type entered in cell B7

    ! Use the if-then-else statement construct

    9/15/1332

    Retrieves the content

    of cell B7

    The If-Then-Else block

    assigns a value of

    cbr to the selected

    soil type

    CEE 3804 : A.A. Trani

  • 8/12/2019 VBA Basics

    33/33

    Last Word Exemption Handling

    ! It is always a good idea to handle anomalous conditionsin your program gracefully

    ! You should always include an Else statement tohandle bad inputs

    !Below we warn the user if the soil could not be matched

    9/15/1333 CEE 3804 : A.A. Trani