Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tutorial 8: Developing an Excel Application
Tutorial 12: Enhancing Excel with Visual Basic for Applications
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Excel and VBA Creating an Excel Application
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Using the Visual Basic Editor Visual Basic for Applications 1.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Adding Automated Functionality to Office Applications.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Chapter 5 new The Do…Loop Statement
CHAPTER SIX Loop Structures.
Chapter 3: Using Variables and Constants
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
Chapter 12: How Long Can This Go On?
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Chapter 2: Using Data.
OCC Network Drives  H:\  P:\ 
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 9 Macros And Visual Basic For Applications.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Programming with Microsoft Visual Basic th Edition
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
IE 8580 Module 4: DIY Monte Carlo Simulation
The Selection Structure
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Introducing VBA Macros
Exploring Microsoft Office Access 2007
Chapter 5 The Do…Loop Statement
Exploring Microsoft Excel
WEB PROGRAMMING JavaScript.
CIS 16 Application Development Programming with Visual Basic
VB.NET PROGRAMMING FINAL EXAM TEST REVIEW.
Introduction to Problem Solving and Control Statements
Presentation transcript:

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use the For...Next loop statement Use the Do...Loop statement Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 1

Launching the VBA Interface To launch the VB Editor in your application: Click the File tab, click Options, click Customize Ribbon, click the Developer check box in the Main Tabs list, and then click OK. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 2

Using the VB Editor Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 3 The VB Editor for an Excel Workbook Menu bar and toolbar Properties window used to set properties for objects. Code Window The Project Explorer lists the Workbook name

Project Explorer The left side of the VB Editor contains the Project Explorer, which is similar to the Windows Explorer. A project is a collection of modules and objects needed to run an application. A module is a container to organize programming code. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 4

Properties Window The Properties window displays the properties or attributes for the currently selected object in the Project Explorer. The Properties window is used to set properties for an object that you can change or set a value. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 5

Code Window The Code window is a text editor for writing and editing VBA programming statements. You enter your code statements in design time. VBA executes the procedure in run time. A procedure is a named sequence of programming statements that performs a set of actions. – Programming statements include: procedures, variable and constant declarations, and mathematical expressions. – When you create several macros in a workbook, Excel stores each macro as a procedure in one module. – The name you enter when you record a macro becomes the name of an individual procedure in VBA. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 6

Identify Code in the Code Window Procedure name A procedure is a named sequence of programming statements that performs a set of actions. Programming statements include: procedures, variable and constant declarations, and mathematical expressions. Comments Keywords - text or a symbol used for a specific purpose in a programming language Indented VBA statements Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 7

Creating Procedures An event is an action, such as the user clicking a button at run time that triggers a program instruction A sub procedure is a procedure that performs an action but does not return a specific value, such as: Sub Display_Message () Indented VBA statements End Sub A function procedure performs an action and returns a value. A property procedure creates or manipulates a custom property. A public procedure is available to any object in the application. A private procedure is available only to a specific object or module Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 8

Getting Help and Debugging Errors Use Help to learn more about VBA. Explanation of terminology Examples of programming code For specific context assistance, click on a keyword, such as Sub, and then press F1. The Debug menu helps you identify errors in programming code Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 9

Declaring and Using Variables Variables are programmer-defined names. Variables can store values that can change while the application is running. Variables store values in computer memory. A program code statement can change the value at any time. A value for a variable exists only while a program is running. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 10

Declaring and Using Constants Constants store values that are specified at design time. Constants remain the same while the application is running. Programmers can change a constant’s value at design time. Types of constants include: – Intrinsic—specific to an application – User-defined—created by a programmer – Conditional compiler—defined in the host application Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 11

Select a Data Type All variables and constants have a data type. The data type refers to the type of data the variable or constant can hold. The data type defines how the data is stored in the computer’s memory. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 12

Select a Data Type (Cont.) Common VBA data types include: – Decimal—Value that contains decimal numbers scaled by a power of 10 – Double—Double-precision floating-point numbers with 14 digits of accuracy – Integer—Whole numbers, no decimal points or places behind the decimal – String—Alphanumeric data including letters, numbers, spaces, punctuation, and other characters with a fixed length Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 13

Declaring Variables and Assigning Values The declaration (Dim) statement assigns: – Name – Data type – Allocates memory to store a value for the variable or constant Variables named with the Public prefix can be used by all procedures. The Scope specifies which statements can access a variable or constant. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 14

Creating an Input Box To obtain data from the user, create an input box. The InputBox function (InputBox) prompts the user to enter a value. The MessageBox function displays the resulting message. Concatenate is the process of joining two or more text strings. Use the ampersand character (&) or plus sign (+) to concatenate, or join, two values, such as MsgBox "Last Name is: " & strLastName shown in this example. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 15

Convert Strings to Numeric Values User input using the InputBox function is returned to the procedure as a text string by default. Data for calculations must be converted to numeric data by using the Val Function. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 16

Performing Calculations Create arithmetic expressions using variables and mathematical operators. Order of precedence is the order arithmetic expressions are performed. Combine multiple operations with parentheses ( ). Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 17

Format Output Results Use Format function to format results of calculations. Uses predefined formats to change the appearance of text. Examples: – Currency – Decimal – Percent Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 18

Common Programming Structures Sequence structures execute statements in the order they appear. Decision structures make comparisons between values, variables, and/or constants and then execute statements based on the comparison. Repetition structures repeat the execution of a series of statements at run time. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 19

Using Decision Structures If…Then statements represent the simplest type of decision structure. If…Then…Else statements test for a condition and specify one option if the test evaluates to True and another if it evaluates to False. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 20

Create a Select Case Statement The Select Case statement compares an expression or a value to a set of cases, situations, or ranges. A case is an individual condition to test. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 21

Repetition Structures Repetition structures complete an operation a specific number of times. Generally while a certain condition is true or until a condition tests true Two common repetition structures – For…Next loop – Do…Loop Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 22

Using the For…Next Loop Statement The For…Next statement repeats: – Loop—set of statements or a procedure – A specific number of times The counter keeps track of the number of iterations through the loop. The step value is the number the counter is incremented or decremented during each iteration of the loop. 23

Using the Do…Loop Statement A Do…Loop statement executes a block of statements while: – A condition remains true – Or until a condition is true Can be written with a pretest or posttest loop A pretest performs the logical test first and executes the code within the loop if the test is true. A posttest executes the code within the loop one time and then performs the logical test to determine if the loop iterates again. 24

Loop Until a Condition Becomes True Do Until loop repeats the iterations of the loop until the specified condition evaluates to True. Can be written with a pretest or posttest loop Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. 25