Civil Engineering Computation

Similar documents
VBA Excel 2013/2016. VBA Visual Basic for Applications. Learner Guide

QUICK EXCEL TUTORIAL. The Very Basics

This chapter is intended to take you through the basic steps of using the Visual Basic

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Candy is Dandy Project (Project #12)

Advanced Excel Macros : Data Validation/Analysis : OneDrive

Creating an expenses record spreadsheet in Excel


Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Agenda. Spreadsheet Applications. Spreadsheet Terminology A workbook consists of multiple worksheets. By default, a workbook has 3 worksheets.

Excel 2010: Getting Started with Excel

Excel 2010: Basics Learning Guide

Microsoft Excel 2010 Handout

BASIC MACROS IN EXCEL Presented by IGNACIO DURAN

Basic tasks in Excel 2013

Excel Level 1

Excel Basic: Create Formulas

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Rev. C 11/09/2010 Downers Grove Public Library Page 1 of 41

Welcome to Introduction to Microsoft Excel 2010

Microsoft Excel 2007

Using Microsoft Excel

Using Microsoft Excel

Intro. Scheme Basics. scm> 5 5. scm>

Exploring extreme weather with Excel - The basics

Part II: Creating Visio Drawings

My First Cocoa Program

Using Microsoft Excel

Introduction to Excel 2013

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4

Using Excel 2011 at Kennesaw State University

A PRACTICAL TUTORIAL TO EXCEL

Chapter 2 The SAS Environment

EXCEL BASICS: PROJECTS

Excel 2013 for Beginners

FACULTY AND STAFF COMPUTER FOOTHILL-DE ANZA

Intro to Excel. To start a new workbook, click on the Blank workbook icon in the middle of the screen.

CS 200. Lecture 07. Excel Scripting. Excel Scripting. CS 200 Fall 2016

CS 200. Lecture 07. Excel Scripting. Miscellaneous Notes

Cell to Cell mouse arrow Type Tab Enter Scroll Bars Page Up Page Down Crtl + Home Crtl + End Value Label Formula Note:

Excel 2016 Basics for Windows

CS 200. Lecture 05. Excel Scripting. Excel Scripting. CS 200 Fall 2014

EXCEL + POWERPOINT. Analyzing, Visualizing, and Presenting Data-Rich Insights to Any Audience KNACK TRAINING

Advanced Financial Modeling Macros. EduPristine

CS 200. Lecture 05! Excel Scripting. Miscellaneous Notes

CS 200. Lecture 07. Excel Scripting. Excel Scripting. CS 200 Spring Wednesday, June 18, 2014

Advanced Excel Charts : Tables : Pivots

Amacro is a sequence of commands or keystrokes that Word records

Getting Started with. Office 2008

Activity 1 Creating a simple gradebook

CS 200. Lecture 07. Excel Scripting. Miscellaneous Notes

DOWNLOAD PDF MICROSOFT EXCEL ALL FORMULAS LIST WITH EXAMPLES

Introduction to Microsoft Excel

Within the spreadsheet, columns are labeled with letters and rows are labeled with numbers.

Payment Function Exercise

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

The clean-up functionality takes care of the following problems that have been happening:

Introduction to Excel

Intermediate Excel 2013

EXCEL BASICS: MICROSOFT OFFICE 2010

Microsoft Excel 2013: Part 3 More on Formatting Cells And Worksheet Basics. To apply number formatting:

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

Introduction to Microsoft Excel 2010

A cell is highlighted when a thick black border appears around it. Use TAB to move to the next cell to the LEFT. Use SHIFT-TAB to move to the RIGHT.

The Excel Project: Excel for Accountants, Business People... from the Beginning Duncan Williamson

Microsoft Excel 2007 Macros and VBA

Chapter 5 Making Life Easier with Templates and Styles

Introduction to Microsoft Excel 2007

EXCEL BASICS: MICROSOFT OFFICE 2007

MAKING TABLES WITH WORD BASIC INSTRUCTIONS. Setting the Page Orientation. Inserting the Basic Table. Daily Schedule

Computer Basics: Step-by-Step Guide (Session 2)

Prepared By: Graeme Hilson. U3A Nunawading

Introduction to Programming with JES

I OFFICE TAB... 1 RIBBONS & GROUPS... 2 OTHER SCREEN PARTS... 4 APPLICATION SPECIFICATIONS... 5 THE BASICS...

KNACK TRAINING. MICROSOFT OFFICE: TIPS & TRICKS FOR EFFICIENCY

Excel Basics 1. Running Excel When you first run Microsoft Excel you see the following menus and toolbars across the top of your new worksheet

= 3 + (5*4) + (1/2)*(4/2)^2.

Basic Microsoft Excel 2007

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2010

Microsoft Excel Level 2

Intermediate Excel 2003

Microsoft Excel 2007 Lesson 7: Charts and Comments

-Using Excel- *The columns are marked by letters, the rows by numbers. For example, A1 designates row A, column 1.

WEEK NO. 12 MICROSOFT EXCEL 2007

Intermediate Excel Training Course Content

DOWNLOAD PDF MICROSOFT OFFICE POWERPOINT 2003, STEP BY STEP

Excel 2016 Basics for Mac

Beginning Excel. Revised 4/19/16

Microsoft Excel 2010

Introduction to Access 97/2000

Practice Exercises for Introduction to Excel

Excel & Business Math Video/Class Project #01 Introduction to Excel. Why We Use Excel for Math. First Formula.

Formatting Values. 1. Click the cell(s) with the value(s) to format.

Microsoft Excel Basics Ben Johnson

4) Study the section of a worksheet in the image below. What is the cell address of the cell containing the word "Qtr3"?

Excel 2007 Fundamentals

Workbooks & Worksheets. Getting Started. Formatting. Formulas & Functions

Excel Template Instructions for the Glo-Brite Payroll Project (Using Excel 2010 or 2013)

Tips & Tricks: MS Excel

Transcription:

Civil Engineering Computation First Steps in VBA Homework Evaluation 2 1

Homework Evaluation 3 Based on this rubric, you may resubmit Homework 1 and Homework 2 (along with today s homework) by next Monday at class time. VBA Functions 4 2

VBA Functions 5 Terminology Input l Information coming into the program from The spreadsheet The user (keyboard or screen) An external file 6 3

Terminology Output l Information going out of the program to The spreadsheet The user (screen) An external file 7 Terminology Operations l Manipulations performed on data or informaiton of three types Arithmetic (+, -, *, /, ^) Relational (>, <, <=, >=, <>, =) Logical (And, Or, Not) 8 4

Terminology Variables Shorthand names for locations in computer memory where information is stored l The type of information can be either specified or unspecified l You will not have to know the physical address of the information, just the Shorthand ID (variable name) l 9 Terminology Decisions A method of controlling which statements (actions) that a program will take based on some logical (yes/no) condition that you specify l This is where we will start today l 10 5

Terminology Loops A method of repeating a set of instructions a number of times or until a logical condition is attained l And we will look at these also l 11 The VBA Interface in EXCEL This is the way that EXCEL would normally appear when you first start it. If you are on the University s computers, this is the way it will always appear when you start EXCEL. 12 6

The VBA Interface in EXCEL Once again, we will go to the upper left corner of the screen and go to the Excel Options box. 13 The VBA Interface in EXCEL We are going to enable a new tab in the ribbon, the Developer tab by checking this box and then clicking OK down below. 14 7

The VBA Interface in EXCEL The Developer Tab now shows up in the ribbon. Go ahead and select that tab from the ribbon. 15 The VBA Interface in EXCEL We get a set of tools that can be used in developing VBA programs. VBA programs are called Macros. This is just Microsoft speak for program. You will probably hear me use program and macro both. 16 8

The VBA Interface in EXCEL We will start by putting a value in Cell B2 of our workbook. The value isn t important, it is the idea of developing a repeatable task we want to focus on here. We are going to change the font, number display, and color of the cell and let Excel look over out shoulder while we do. 17 The VBA Interface in EXCEL We start by selection the Record Macro option in the toolbar. Before you check this selection, make sure that the cell we are going to work on is selected in the workbook. (Notice the black bounding box around the cell). 18 9

The VBA Interface in EXCEL You should see this screen show up on your workbook. In this case, we can name the Macro ClassExample. Notice that there is no space in the name. We could assign a control key sequence to the macro but in this case we won t. If you are going to do something lots of times, it may help to make that assignment. 19 The VBA Interface in EXCEL Notice that once you clicked OK, the toolbar changed. Now you have an option to Stop Recording where you had the Record Marco option before. Until you choose Stop Recording, everything you try to do on the spreadsheet will be recorded and interpreted into the macro you just named. Don t check this just yet. 20 10

The VBA Interface in EXCEL If you right click your mouse with the cell selected (which it already was), you see options for displaying the information in the cell. 21 The VBA Interface in EXCEL Change the fill color of the cell to Green. 22 11

The VBA Interface in EXCEL Change the background (fill) to green, the text color to yellow, the font to Arial, made it bold and italic and size 14, and increased the decimals shown to three. Your cell should look something like this. 23 The VBA Interface in EXCEL Now go ahead and choose the Stop Recording option. 24 12

The VBA Interface in EXCEL Put a number into another cell. It really doesn t matter what the number is. Make sure the cell is selected. 25 The VBA Interface in EXCEL Select the Macros option from this ribbon. If you have a problem with this, we may need to address Macro Security. 26 13

The VBA Interface in EXCEL This is a list of the macros current available to this workbook. In our case, it probably only have one, ClassExample. If it isn t highlighted, select it. Then hit the run button. 27 The VBA Interface in EXCEL The macro that you recorded, then ran, has duplicated the changes you made to Cell B2 in cell B5. 28 14

The VBA Interface in EXCEL Now we can look at the code in the macro and start getting an idea about what is under the hood in Excel. 29 The VBA Interface in EXCEL This time, we select the Visual Basic option from the Developer toolbar. 30 15

The VBA Interface in EXCEL This is the VBA development interface. Right now it is empty because we haven t asked it to display anything. Don t worry about knowing about the interface just now. We will go through what we need as we need it. 31 The VBA Interface in EXCEL Everything in VBA is grouped together in VBAProjects. You should be able to see a list of things for this Project (similar to a Workbook). There are three Worksheet objects and a This Workbook object. There is also something called Modules. Modules are where the VBA macros reside. 32 16

The VBA Interface in EXCEL Click on the + sign next to the Modules folder. This will open up the modules to make them available for you to view. 33 The VBA Interface in EXCEL You should see that you have a Module named Module 1. Go ahead and double click on the Module 1 name. 34 17

The VBA Interface in EXCEL Here is your first example of VBA code. Because Excel developed it from your keystrokes, it is very complicated. Your code won t look like this very often. We won t talk much about this code because there is just too much extraneous stuff in it. Just know that you can do some things this way. 35 The VBA Interface in EXCEL The keystroke capture is not a good way to write code but it can often be a good way to start writing code for a problem or project. 36 18

Writing a VBA program The first few programs that we are going to write will be very simple and not of much use. This is to start with things we know how to do and see what they look like in VBA. Please don t judge the power of VBA by these first few programs. 37 Writing a VBA program To start our program, we need to be in the Visual Basic part of Excel. If you haven t closed the Module1 from the last part we did, please do so now. You should see a screen like the one shown on the next slide. 38 19

39 With nothing else showing, we select the Insert option from the ribbon. This will give us a set of options. 40 20

With nothing else showing, we select the Insert option from the ribbon. This will give us a set of options. Select the Module Option. 41 This should be what you now see in the section of your screen. Notice that the Module is Module2. Module1 was where the code generated by Excel was held. As you create modules, they are sequentially numbered. 42 21

This is just the starting point to hold our macros. We now need to put out first macro into place. To do this, we will go to the Insert menu again but this time we will insert a Procedure. 43 We now have a window where we can set up a unique name for our macro. It cannot start with a number or symbol and it cannot contain any blanks. 44 22

Here I named the macro FirstMacro. For now, we are going to leave the other parts just as they are. We will look at the difference between a Sub and a Function in a later class. 45 Once we accepted the macro by clicking the OK button, EXCEL VBA put in the first and last line of our Sub macro. You don t have to do that unless you want to. It is usually just easier to let EXCEL do the work. 46 23

We are going to start by writing a macro to sum numbers in the first column of the sheet. We are going to sum the numbers in cells A2, A3, and A4 and put the result in A6. 47 We start by making four variables. Three to hold the information coming in from the spreadsheet and one to hold the information going to the spreadsheet from the macro. Variables are just places to store information 48 24

We set up these locations by Typing the variables. Typing is the method we use to tell VBA what type of information we are going to store in the location named by the variable name. In this case, we are going to use the Single variable type. 49 Nearly all non-trivial VBA code involves declaring variables. While VBA allows a great deal of flexibility in declaring your variables, this flexibility can easily lead to poor coding practices. 50 25

As the first line of code in the module, above and before any other lines, use: Option Explicit This statement requires that all variables be declared using a Dim statement. 51 Now we will tell VBA that we are going to use four variables FirstNum, SecondNum, ThirdNum, and SumOfNumbers. We do this by using the Dim (short for Dimension). The general form for the statement is DIM variablename As variabletype 52 26

So in our program (after the Public Sub statement) we add four lines defining our variables. DIM variablename As variabletype 53 This didn t do anything. There is nothing stored in these variables right now. They are empty locations just waiting to have something put into them. They also only exist while the macro is executing (running). When it gets to the End Sub statement, everything in them is lost. 54 27

Now we can move some information off the worksheet into the program. We do that with an Assignment statement. In VBA, an assignment statement is identified by an =. In English it would read Calculate the right side of the expression and store the result in the left side of the expression. 55 The left side of the expression must be 1. A valid variable name (with Option Explicit it must also have been typed in the Sub). 2. A valid location on a workbook You cannot make a calculation on the left side of the = and you cannot assign something to a constant on the left side. 56 28

A = 24 (Valid) A = B + C (Valid, assuming all the variables are valid) 1 = A + 3 (Invalid, constant on left side) B + C = A + 3 (Invalid, not a variable or cell location on left side) The = sign in this context is not the same as the algebraic = sign. It does not mean is equal to. 57 No we are going to get some information from the workbook and put it into variables in the macro. 58 29

Notice how we addressed the cells. We used a Range object with the argument that was the address of the cell on the workbook. 59 It is critical that you use the double quotes. This is known as a string constant. If you just use A2 with the quotes, it will think you are looking for the variable A2. 60 30

The.Value after the argument is the property of the object that you are going to use. In this case it is the value in the cell. 61 So the FirstNum = Range( A2 ).Value tells VBA Find the cell A2 on the worksheet Get the value that is in that cell Store that value in the variable name FirstNum 62 31

This is another way of taking information from cells in a worksheet. In this case, we locate information in the worksheet by using a Cells object. 63 Now we will add up the values in the three cells and store the sum in the variable SumOfNumbers. 64 32

SumOfNumbers is only inside the program. To be able to see what is in there, we will have to send the value back to the worksheet. 65 Once again we use the assignment statement. 66 33

Once again we use the assignment statement, this time using the Cells object rather than the Range. 67 We have to save this program before we run it. This is to help us in case we have a dramatic error and can t get the program to stop running. Save often and save early. 68 34

Now we can see if our program runs. We can actually do this from the VBA window. 69 This is the run icon on the toolbar. You can also see a Run option above. Either one should work. 70 35

When you try to run your macro, you may well see this error message. EXCEL is trying to protect you from malicious code. Macros are very powerful things so you have to reset your EXCEL to allow that macro to run. 71 Back on the worksheet (not in VBA) if you have the Developer tab open, in the Code box, you should see a Macro Security option. 72 36

When you selected the Macro Security option, you are given to this page. For now, we will select the Enable all macros. If you are on your own machine, be sure to disable this before you run anything that you don t write yourself if you don t know where it came from. 73 After you have saved, closed, and reopened your sheet, go back to VBA, and put your cursor somewhere in the Sub. Then use the run icon again. 74 37

Hopefully you didn t see anything happen. If you were asked to select a macro, just select the name you used for this particular Sub and run it. You will not see any results on this page. 75 If you go back to the worksheet, you should see results. 76 38

If you go back to the worksheet, you should see results. 77 Program Execution Remark statements serve as notes for the programmer (and possibly the user). Do not change the execution of the program or make any changes to the program environment. 78 39

Program Execution Notice that you can use a single quote to specify a remark statement or you can use the Rem Key Word. You can also put remarks on the same line after an executable statement but I really don t recommend doing this. 79 Program Execution These are specification statements. They set up the program environment. Here they are used to name and type the variables we are going to use in the program. There are other types of specification statements we may use later. They are set up in the order that they are encountered. 80 40

Program Execution These are executable statements. They do something. In this case, we have five assignments statements that get data from the worksheet, make a calculation, and then put data back on the worksheet. 81 Program Planning One of the most difficult concepts for beginning programmers to understand is the need to plan the program out before you begin developing the code This is especially true in a class where most of the problems do not seem to require a lot of planning 82 41

Program Planning The development and testing of a plan, before the code development is one of the most time saving (and frustration saving) steps that you can take When you have written code for a few years, for simple programs you may be able to skip this step but you probably won t because you will understand how useful it is 83 Program Planning Program planning is where you will develop the logic of your solution and test it before you worry about the syntax of the programming language If you skip this step, you will have two problems to deal with Syntax l Logical flow l 84 42

Program Planning Think of the logical flow as deciding what you want to do and in what order you want to do it Then syntax is getting the computer to follow your pattern of instructions 85 A Decision Problem Now we can look at a somewhat more sophisticated problem and work through the process of program development. We are given a worksheet with numbers in three cells. We need to decide which of those three numbers is the largest. Once that is decided, we will divide each of the original numbers by the largest. 86 43

A Decision Problem 87 And display each of the results of the divisions as a percentage next to the original location of the number. A Decision Problem Up to this point, we have not had to make any decisions in our programs They were just fine executing from top to bottom Now we will have to move to make a choice on how to execute because we will have alternatives 88 44

The IF statement Now that we know that we need to be able to make a decision, we need to know how it is done in VBA You may see it referred to as the IF statement or as the IF-THEN-ELSE-ENDIF construct They are actually both the same just different ways of referring to the process 89 The IF statement There are two forms of the syntax and you can't mix them. It is possible to write everything on one line (usually not as nice because it is a bit harder to read and follow) The syntax is l 90 IF question THEN action; ELSE other action 45

The IF statement The syntax is l IF question THEN action; ELSE other action The first action is what we would do if the answer to the question was yes The other action is what we would do if the answer to the question was no Notice the semicolon in the statement 91 The IF statement The syntax is l IF question THEN action; ELSE other action It is not necessary to have the ;ELSE other action part of the statement Sometimes you don t want to anything is the answer to the question is no 92 46

The IF statement The syntax is l IF question THEN action; ELSE other action The form of the question is usually developed using a relational operator (>, <, <=, >=, =, <>) We compare the values of two arguments (constants or variable) using the relational operator. 93 The IF statement There is another form of the statement that allows for a bit more flexibility This is not the full version but it will do for now. IF condition THEN l Action l ELSE l Other Action l END IF l 94 47

The IF statement Notice that we use more than one line for this form and that we have to have something telling use we are done IF condition THEN l Action l ELSE l Other Action l END IF l 95 The IF statement There is more flexibility here because the action or other action can be another IF IF condition THEN l Action l ELSE l Other Action l END IF l 96 48

The IF statement We can start by recognizing that we need three variables to store the original data we bring in from the worksheet. 97 The IF statement We have now completed the input from the worksheet and stored the values the worksheet into three variables. Now we need to start with the decision tree. 98 49

The IF statement We are asking the first question. The next statement will be the action we take if the question is answered yes. 99 The IF statement Following out flowchart, the action we took if the answer was yes was to ask another question. Notice that I indent the action in an IF statement, this makes things easier to follow especially when you are searching for problems in your code. 100 50

The IF statement We don t have anywhere to store the largest value when we find it, so we need to add a variable to store this largest value. 101 The IF statement Now we are on the yes side of this question so we store the first number value in to the variable Largest. 102 51

The IF statement We need to show that we are done with the actions from the yes branch so we insert the keyword ELSE on the next line. 103 The IF statement Notice that the indentation of the Else matches up with the If that spawned it. The Else matches with the question where it is the No answer. 104 52

The IF statement The No answer branch stores the third number into the variable Largest. Therefore this statement should follow the Else Statement. 105 The IF statement Now if we don t close this Else, the program will think we are continuing on with actions from when the current question was answered with a No. To stop this, we add and End If statement which aligns with the IF and Else statements for this question. 106 53

The IF statement And we need to do the actions on the No branch of the FirstNum > SecondNum question. 107 The IF statement And we need to do the actions on the No branch of the FirstNum > SecondNum question. 108 54

The IF statement And to finish it off, we have to close the first If with and End If. 109 The IF statement Looking at the If blocks. 110 55

The IF statement 111 The IF statement 112 Now we are through with the decision blocks we can finish up the program by making the calculations and putting the results onto the workbook. And we test the macro after saving the workbook. 56

114 57

115 116 58

117 118 59

119 120 60

121 122 61

123 124 62

125 126 63

127 128 64

129 130 65

131 132 66

133 134 67

135 136 68

137 138 69

139 140 70

141 142 71

143 144 72

145 146 73

147 148 74

149 150 75

151 152 76

153 154 77

155 156 78

157 79