Programming in Visual Basic

Slides:



Advertisements
Similar presentations
ISOM3230 Business Applications Programming
Advertisements

PROGRAMMING IN VISUAL BASIC PART 1
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Introduction to C Programming
Fundamentals of Python: From First Programs Through Data Structures
Computer Science 101 Introduction to Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fundamentals of Python: First Programs
Languages and Environments Higher Computing Unit 2 – Software Development.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Introduction to Python
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Computer Science 101 Introduction to Programming.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CPS120: Introduction to Computer Science Operations Lecture 9.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Controlling Program Flow with Decision Structures.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Software Engineering Algorithms, Compilers, & Lifecycle.
Bill Tucker Austin Community College COSC 1315
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
2.5 Another Java Application: Adding Integers
Primitive Data, Variables, Loops (Maybe)
The Selection Structure
Variables, Expressions, and IO
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 - Introduction to C Programming
Chapter 10 Programming Fundamentals with JavaScript
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
Chapter 2 - Introduction to C Programming
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Programming in Visual Basic Class 2 Programming in Visual Basic

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

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

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

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

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

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

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

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

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

Creating a VBA program

Step 1 – Open Excel

Step 2 – Save as… Save as … Example_1

Step 3 – Open VBA Editor Press… Alt F11

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

Step 5 – Insert module Left click Insert Left click Module

A blank form will appear This is where the program goes

Step 6 – Type in program Here is the program

Use your program

Step 1 – Go to Excel spreadsheet Left click Excel button

Step 2 – Enter numbers into cells 4 & 8

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

What happened? = adder(A1,B1) A B C 1 4 8 12 2 Function adder( a, b) 4 8 12 Function adder( a, b) adder = a + b End Function

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

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

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.

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, 3.14159, – 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

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, 3.14159, – 0.5 “Floating point” numbers are harder to represent in a computer 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

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

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

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.

What if a line is too long? Example… var = 1 + 2 + 3 + 4 + _ 5 + 6 + 7 + 8 Is evaluated as var = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 The underscore joins lines together.

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

How to protect against spelling errors This statement requires that all variables be declared 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 Do the following: Add Option Explicit to your program Misspell c = 5 as d = 5 Go to the Excel spreadsheet and try to use adder

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

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

Selection: If ? Function comparison(a, b) comparison = 0 Y Calc A 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.)

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

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

Repetition: Do While Input Function do_while_loop(a As Integer) Title 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 Title Declare variables Initialize variables Calculate Output Enter the above program and test it in Excel.

Repetition: Do While As a flowchart… i = 1 j = 1 N i < a? Y 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 i = 1 j = 1 i < a? i = i +1 j = j *2 Y N

Repetition: For As a flowchart… i = 1 j = 1 Default: Y Counts by +1 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 i = 1 j = 1 i > a? i = i +1 j = j *2 N Default: Counts by +1 Y Enter the above program and test it in Excel. Note: The code is shorter, but the flowchart is the same.

Repetition: For As a flowchart… i = a j = 1 Overrides default: Y 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 As a flowchart… i = a j = 1 Overrides default: Counts by -1 i < 1? Y N Enter the above program and test it in Excel. i = i – 1 j = j *2

Bring Math Supplement to next class.