vba principles - 128.173.204.63

32
2/14/22 CEE 3804 1 VBA Principles CEE3804: Computer Applications for Civil and Environmental Engineers

Upload: others

Post on 05-Jul-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VBA Principles - 128.173.204.63

2/14/22 CEE 38041

VBA Principles

CEE3804: Computer Applications for Civil and Environmental Engineers

Page 2: VBA Principles - 128.173.204.63

2/14/222

Objectivesl Develop a simple set of programs in VBAl Learn to execute a macro from within a

worksheetl Introduction to loop structures

CEE 3804

Page 3: VBA Principles - 128.173.204.63

2/14/22CEE 38043

Before You Programl Develop a strategy to solve the probleml Develop pseudo-codel Develop a flowchart (helps guide your thought process)

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

code can help you understand bad logic

Start

Outputc

Inputb

c = a + b

Inputa

End

Start here:

Flowchart Then code

Page 4: VBA Principles - 128.173.204.63

2/14/22CEE 38044

Flowchart Symbolsl Review them in Chapra’s book (page 115)

Symbol Name Function

Terminal Represents the beginning of end of a program

Flow lines Represent the flow of logic

Process Calculation of data manipulation

Input/Output Represents input and output of data and information

Decision Represents a comparison or a condition with more than one path

Start

c = a + b

Inputb

If (a>b) True

False

Page 5: VBA Principles - 128.173.204.63

2/14/22CEE 38045

Flowchart Symbols (cont.)

Symbol Name Function

Count-controlled loop

Indicates the number of times a loop is excecuted

Junction or connector

Represents a confluence of flow lines

Off-page connector Break or continuation to another page

Stored data Represents data storage in a database

Page 6: VBA Principles - 128.173.204.63

2/14/22CEE 38046

A Simple VBA Programl Lets develop a simple program in VBA to

calculate the sum of two numbers of a worksheet

l The program should retrieve two numbers from “sheet1” in a standard Excel worksheet

l The program should add the two numbers and return the result to the worksheet

l Place a “run” button in the worksheet to facilitate future execution of the subroutine

Page 7: VBA Principles - 128.173.204.63

2/14/22CEE 38047

Program Flowchartl Here is a basic flowchart to build this program

Start

Outputc

Inputb

c = a + b

Inputa

End

Start

Outputc

Inputb

c = a + b

Inputa

End

Version 1

Version 2

Page 8: VBA Principles - 128.173.204.63

2/14/22CEE 38048

Excel Worksheet (final program)

Page 9: VBA Principles - 128.173.204.63

2/14/22CEE 38049

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

l Go to: Tools/Macrol Then select VBA

Page 10: VBA Principles - 128.173.204.63

2/14/22CEE 380410

VBA or Macros in Excel WIN 2013

l Go to : Developer Tabl Then Click Visual Basic

Page 11: VBA Principles - 128.173.204.63

2/14/22CEE 380411

VBA Code

Name of subroutine

Go to “sheet1”Select the value of cell B7 and assign it to variable “a”

Select the value of cell B8 and assign it to variable “b”Calculate the value of “c”

Select cell B8 and assign it to variable “c”

Page 12: VBA Principles - 128.173.204.63

2/14/22Copyright 200712

Adding the “Run” Buttonl Adding the “run” button requires that you insert

a control button and then assign a macro to subroutine “adder”

Page 13: VBA Principles - 128.173.204.63

2/14/22CEE 380413

Assigning the Behavior to the “Run” Button

l Assign the macro “adder” from the list of available macros to button “Run”

l Right-click on the button to assign a behavior

Page 14: VBA Principles - 128.173.204.63

2/14/22CEE 380414

Test Your Programl Test the program to make sure it works

l Try variations of the program as well

Page 15: VBA Principles - 128.173.204.63

2/14/22CEE 380415

Subroutine Adder1 with Input Boxl This variation of the program creates an input

box for the first number

String variable “a”is read using an input box

Returns the number contained in the string “a”

Page 16: VBA Principles - 128.173.204.63

2/14/22CEE 380416

Excel Help for VAL Function

Page 17: VBA Principles - 128.173.204.63

2/14/22Copyright 200717

Executing the New Program

Page 18: VBA Principles - 128.173.204.63

2/14/22CEE 380418

A Simple Program with a Loopl Loops are natural ways to execute

computations that require multiple iterationsl Loops can be conditioned or controlled by a

counter

l Conditional loops - when some condition is used to exit the loop

l Counter controlled loops - when the number of passes in the loop is known

Page 19: VBA Principles - 128.173.204.63

2/14/22Copyright 200719

First Program with a Loop

Page 20: VBA Principles - 128.173.204.63

2/14/22CEE 380420

The VBA Code Behind

Loop code

Page 21: VBA Principles - 128.173.204.63

2/14/22CEE 380421

A Loop with Concatenation Controll The program in worksheet: loopConcatenate.xlsoffers a sample of a loop computation and the

use of concatenation control to estimate pavement thicknesses

l The pavement thickness function created in previous classes in “called” by the VBA code

Page 22: VBA Principles - 128.173.204.63

2/14/22CEE 380422

Worksheet Interface

Cell B8 controls the number of times the loop is executed

Page 23: VBA Principles - 128.173.204.63

2/14/22Copyright 200723

The Code Behind the Worksheet

Page 24: VBA Principles - 128.173.204.63

2/14/22CEE 380424

Code (cont.)

Calculates pavement thickness

Concatenation

Page 25: VBA Principles - 128.173.204.63

2/14/22CEE 380425

Try Other Refinementsl Currently the loop counter just overwrites the

values of pavement thickness without erasing previous computation

l Try adding a line or two of code to erase the previous table of computations while executing the 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

Page 26: VBA Principles - 128.173.204.63

Simple Control Structurel If-Then-Else is a powerful, yet simple control

structure that could be used to make “branching” decision in a program– If (condition) then

l Code 1 is executed– Elseif (condition 2) then

l Code 2 is executed– Elseif (condition 3) then

l Code 3 is executed– End if

l You can add as many else-if-then statements as needed in your program logic

2/14/22CEE 380426

Page 27: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

l Suppose we would like to modify the previously created program to replace the value of California Bearing Ratio (CBR) for the type of soil used in construction

l Suppose we have a simple table with typical values of CBR as follows:

2/14/22CEE 380427

Material Value of CBR (dim)Crush stone 100Sandy soil 25Silty soil 13Clay soil 7Organic soil 4

Page 28: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

l Recall the original spreadsheet

2/14/22CEE 380428

Material Value of CBR (dim)Crush stone 100Sandy soil 25Silty soil 13Clay soil 7Organic soil 4

Page 29: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

l Create a list of items to be selected by the user in cell B7

l Use the Data Validation in the Data Tabl Create a list in the validation criteria

2/14/22CEE 380429

Page 30: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

l The list of items can be enumerated in another section of the spreadsheet

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

2/14/22CEE 380430

Page 31: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

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

2/14/22CEE 380431

Page 32: VBA Principles - 128.173.204.63

Modification of Pavement Analysis Program (Loop with Concatenation)

l Complete the VBA code to assign the value of CBR for a selected soil type in cell B7

l Use the if-then-else statement construct

2/14/22CEE 380432

Retrieves the content of cell B7

The If-Then-Else blockassigns a value ofcbr to the selectedsoil type