Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.

Slides:



Advertisements
Similar presentations
1 Overview Introduction to VBA The Visual Basic Editor (VBE) –First Program Getting Started with VBA –Subroutines –Declaring variables –Input boxes and.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
String Variables Visual Basic for Applications 4.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Chapter 3: Using Variables and Constants
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CS0004: Introduction to Programming Input and Output.
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.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Enhancing User Interaction Through Programming
Creating Macros Using VBA. Assigning a Macro to a Button Display the Forms toolbar. Click the Button icon. Click and drag the mouse pointer to specify.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Visual Basic for Applications Macro Programming For Microsoft Office.
Week 2.  Macros revisited  The VBA Editor  The object model  Using variables  If statements.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Chapter 9 Macros And Visual Basic For Applications.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ME 142 Engineering Computation I Using Subroutines Effectively.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 6 Controlling Program Flow with Looping Structures.
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
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Scripting
Using Procedures and Exception Handling
Microsoft Office Illustrated
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Excel
WEB PROGRAMMING JavaScript.
Additional Topics in VB.NET
CHAPTER FOUR VARIABLES AND CONSTANTS
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Chapter 4 Getting Started with VBA

Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a macro. A sub is any set of code that performs a particular task. Sub can contain one line of code or hundreds. Subs can be used to break up a lengthy program into smaller more manageable parts. You can have a main sub that calls other subs or functions. Program is a collection of subs that achieve an overall goal. Each sub has a name which can be a mixture of letters, numbers, and the underscore character. All subs must start with the key work Sub and end with End Sub. Remember, all reserved words are colored Blue in VBA. The parenthesis next to a sub’s name are for arguments and are generally used with private subs that are not the main sub. It is common practice to place several related subs in the same macro or module. You can execute the macro using the Run button in the VBA editor, use the F5 key, use the Run/Run Sub/UserForm menu item, or create a button.

Understanding and Declaring Variables Variable are temporary storage areas. They can store values, True/False, and labels. Variable types: –String can store text –Integer stores whole numbers between +- 32,000 –Long stores whole numbers between +- 2 billion –Boolean is a logical variable stores True or False –Single stores real numbers up to seven decimals long –Double stores real numbers up to fourteen decimals long –Currency stores monetary values –Variant can store any other variable type –Dim is the reserved word used to create variables Option Explicit forces all variables used in the macro to declared with the Dim statement Option Explicit should be the first statement entered into the code window Object variable points to an object. This could allow you to use a range name for a several adjacent cells. –Example: Dim SRange As Range – Set SRange = ActiveWorkbook.Worksheets(“Data”).Range(“Scores”) –From that point on you can refer to the range as SRange –When defining an object use the Set statement on the left side of an assignment statement

Input Boxes,Message Boxes, Concatenation, Comments, and Strings The Input Box function takes at least one argument: a prompt. A second argument that is often used is the title that appears at the top of the dialog box. The generic dialog box has OK and Cancel buttons, a title, a prompt, and a text box for the user’s input. The MsgBox function takes at least one argument: a message that you want to display. Two other optional arguments often used are a button indication and a title. Message boxes with Yes and No buttons use the vbYesNo MsgBox InputBox(“Type your name.”,”User’s name”), vbInformation, “User’s Name” will make message box the result of the InputBox function. The underscore character allows you to continue statements on more than one line. Whether to use parentheses depends on whether or not you wish to capture the results from the message function. Parentheses are required when the result is captured in a variable or used in some way. Parentheses are optional when no result is being captured or used in someway. Concatenation character (ampersand &) will bind together two variables, strings, or some combination. Comments are used to document your work and should be used when appropriate. They are denoted by an apostrophe and have the color green. They are non-executable statements and the computer will ignore then. Strings are labels or text mixed with numbers and symbols. When assigning text or labels to strings use quotation marks around the text or label.

Displaying Text and Values Text can be displayed using a message box or by writing the contents of a string variable or literal to a particular cell. Values can be formatted using: Format$(variable,”number of decimals”) Currency formated using FormatCurrency function The Format function can be used for customized formats. String functions Right, Left, Mid, and Len can be used to extract certain parts of the string.

Specifying Objects, Properties, and Methods Specifying a member of a collection use the plural name of the collection, with the particular member specified in parentheses and enclosed inside quotes. Specifying objects down a hierarchy separate them with a period, with objects farther down the hierarchy to the right. An object to the right is qualified by any objects listed to its left. Specifying a property of an object list the property name to the right of the object separated by a period. Specifying a method for an object list the method name to the right of the object separated by a period. With/End With is a shortcut that can be used with objects an their properties and methods. You must use the End With and indent when appropriate. VBA tips: –Screen Updating: Application.ScreenUpdating = False –Display Alerts: Application.DisplayAlerts = False –Timer function: Allow you to see how long it takes the program to execute –StartTime = Timer –Statements –ElapsedTime = Timer – StartTime –MsgBox “This section took “ & ElapsedTime & “ seconds to run.”