Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 2 Programming in Visual Basic. Class Objectives Recognize when programming is required to solve a problem Understand basic structure of a program.

Similar presentations


Presentation on theme: "Class 2 Programming in Visual Basic. Class Objectives Recognize when programming is required to solve a problem Understand basic structure of a program."— Presentation transcript:

1 Class 2 Programming in Visual Basic

2 Class Objectives Recognize when programming is required to solve a problem Understand basic structure of a program Understand function vs. subroutine Know how to create a program Know rules for naming variables Know precedence of arithmetic and comparison operators Understand integer vs real number

3 Class Objectives (con’t) Understand comments Know what to do if a line is too long Know how to protect against spelling errors Selection structures: If, If-Else, If- ElseIf Repetition structures: Do Until, For

4 What is a program? Program – An algorithm implemented within a computer.

5 When do you use a program? Spreadsheets can perform about 95% of the computing tasks; however, they are not suitable for the following: Highly repetitive tasks Decision making

6 What are some programming languages? Basic Cobal Fortran Pascal Ada Java C LISP We will use Visual Basic for Applications (VBA). MatLab?

7 What is Visual Basic for Applications (VBA)? It is a version of Visual Basic that is embedded within Excel.

8 Why are we learning VBA, rather than another programming language? Simple Easy to learn Quickly learned Capitalizes on your knowledge of Excel

9 VBA Interaction with Excel Excel VBA Function 0, 1, 2, 3 … numbers 1 number Excel VBA Subroutine 0, 1, 2, 3 … numbers 1, 2, 3 … numbers Our first focus

10 Sample Function Function adder( a, b) adder = a + b End Function Identifies program as a function Function name Numbers passed into function through arguments Action taken by the function Identifies the end of the function The single number is returned through the function name

11 Creating a VBA program

12 Step 1 – Open Excel

13 Save as … Example_1 Step 2 – Save as…

14 Step 3 – Open VBA Editor Press… Alt F11

15 Step 4 – Identify where to file your program Left-click here

16 Step 5 – Insert module Left click Insert Left click Module

17 A blank form will appear This is where the program goes

18 Step 6 – Type in program Here is the program

19 Use your program

20 Step 1 – Go to Excel spreadsheet Left click Excel button

21 Step 2 – Enter numbers into cells Enter numbers 4 & 8

22 Step 3 – Call the function = adder(A1,B1)

23 What happened? Function adder( a, b) adder = a + b End Function 1212 A B C = adder(A1,B1)

24 Rules for naming variables Must begin with letter Use only letters, numbers, and underscores Do not use space, period (.), exclamation mark (!), or the &, $, # Do not exceed 255 characters in length Avoid names already used by Excel and VBA

25 Operator Precedence in Formulas 1.Parenthesis 2.Negation (–) 3.Exponentiation (^) 4.Multiplication and division (*, /) 5.Addition and subtraction (+, –) answer = (5 + 7^2*3)/4 – 1 Evaluates as… answer = (5 + 49*3)/4 – 1 answer = ( )/4 – 1 answer = (152)/4 – 1 answer = 38 – 1 answer = 37

26 Odd features of computer formulas… In algebra, the following formula has no solution… A = A +3 In computers, the above formula means … Take the current value of A, add 3 to it, and make the result the new value of A. In algebra, either of the following two formulas has meaning… y = x +3 or x + 3 = y In computers, only the first formula is valid.

27 Integers and Real Numbers Integer – Any positive or negative counting number, or zero. Examples: 4, –7, 0 Real Number – Any rational or irrational number Examples: 4.0, 0.75, , – 0.5 Note: Rational number – can be expressed as m/n where m and n are integers and n is not zero Irrational number – cannot be expressed as m/n where m and n are integers and n is not zero

28 Integers and Real Numbers Integer – Any positive or negative counting number, or zero. Examples: 4, –7, 0 Real Number – Any rational or irrational number Examples: 4.0, 0.75, , – 0.5 Note: Rational number – can be expressed as m/n where m and n are integers and n is not zero Irrational number – cannot be expressed as m/n where m and n are integers and n is not zero “Floating point” numbers are harder to represent in a computer

29 Declaring Variables When introducing a new variable into a computer program, you should tell the computer what kind it is. Some common options… Increasing memory requirements Integer Single – single-precision real number Double – double-precision real number

30 Modify you program as follows… Function adder( a As Integer, b As Integer) Dim c As Integer c = 5 adder = a + b + c End Function Declared as integers What happens when you introduce real numbers into the Excel cells?

31 Comments Comments can be added to help humans understand the program. Comments are ignored by the computer. Function adder(a As Integer, b As Integer) ‘ This program adds two numbers together and adds 5 Dim c As Integer c = 5 adder = a + b + c End Function The apostrophe signals the computer to ignore what follows Indentation is also ignored my computer, but helps humans to understand.

32 What if a line is too long? Example… var = _ Is evaluated as var = The underscore joins lines together.

33 Types of Errors Syntax errors – simple errors that do not follow the required format Compiler errors – errors that occur when the computer program is converted to machine code Run-time errors – a program may compile fine, but may fail during a run Logic errors – a program may run fine, but have mistakes in logic that give incorrect answers Spelling error – if you misspell a variable, the computer interprets it as an additional variable and likely will give incorrect answers

34 How to protect against spelling errors Option Explicit Function adder(a As Integer, b As Integer) 'This program adds two numbers together and adds 5 Dim c As Integer c = 5 adder = a + b + c End Function This statement requires that all variables be declared Do the following: 1.Add Option Explicit to your program 2.Misspell c = 5 as d = 5 3.Go to the Excel spreadsheet and try to use adder

35 Your function failed to run and you got an error message. How to recover… 1.Press OK in the error message 2.Correct the spelling 3.Press F5 It is good programming practice to use Option Explicit so that spelling errors are caught.

36 Comparison Operators 1. Equality (=) 2. Inequality (<>) 3. Less than (<) 4. Greater than (>) 5. Less than or equal to (<=) 6. Greater than or equal to (>=) Evaluated in this order of precedence

37 Selection: If Function comparison(a, b) comparison = 0 If a > b Then comparison = 1 End If End Function Enter the above program and test it in Excel. (Note: Option Explicit can appear only once on a sheet, so it is omitted here.) ? Y N Calc A

38 Selection: If-Else ? Y N Function comparison(a, b) If a > b Then comparison = 1 Else comparison = 2 End If End Function Enter the above program and test it in Excel. (Note: Option Explicit can appear only once on a sheet, so it is omitted here.) Calc A Calc B

39 Selection: If-ElseIf Function comparison(a, b) If a > b Then comparison = 1 ElseIf a = b Then comparison = 2 Else comparison = 3 End If End Function Enter the above program and test it in Excel. (Note: Option Explicit can appear only once on a sheet, so it is omitted here.) ? Y N Calc A Calc C ? Y N Calc B

40 Repetition: Do While Function do_while_loop(a As Integer) Dim i As Integer Dim j As Integer i = 1 j = 1 Do While i < a i = i + 1 j = j * 2 Loop do_while_loop = j End Function Enter the above program and test it in Excel. Title Declare variables Initialize variables Calculate Output Input

41 Repetition: Do While Function do_while_loop(a As Integer) Dim i As Integer Dim j As Integer i = 1 j = 1 Do While i < a i = i + 1 j = j * 2 Loop do_while_loop = j End Function As a flowchart… i = 1 j = 1 i < a? i = i +1 j = j *2 Y N

42 Repetition: For Function for_loop(a As Integer) Dim i As Integer Dim j As Integer j = 1 For i = 1 To a j = j * 2 Next i for_loop = j End Function As a flowchart… i = 1 j = 1 i > a? i = i +1 j = j *2 N Y Enter the above program and test it in Excel. Note: The code is shorter, but the flowchart is the same. Default: Counts by +1

43 Function for_loop(a As Integer) Dim i As Integer Dim j As Integer j = 1 For i = a To 1 Step -1 j = j * 2 Next i for_loop = j End Function Repetition: For As a flowchart… i = a j = 1 i < 1? i = i – 1 j = j *2 N Y Enter the above program and test it in Excel. Overrides default: Counts by -1

44 Bring Math Supplement to next class.


Download ppt "Class 2 Programming in Visual Basic. Class Objectives Recognize when programming is required to solve a problem Understand basic structure of a program."

Similar presentations


Ads by Google