University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.

Slides:



Advertisements
Similar presentations
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
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
Excel and VBA Creating an Excel Application
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Introduction To Form Builder
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.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Lab 8 Solver In VBA ► Review – Solver Add-in In Excel ► Solver Add-in In VBA.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
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.
# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become.
Introduction to Engineering Computing GEEN 1300 Lecture 7 15 June 2010 Review for midterm.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
Visual Basic for Applications Macro Programming For Microsoft Office.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-2 1 Lecture Outline Mathematical Expressions String Concatenation Built-in.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Chapter Two Creating a First Project in Visual Basic.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Dialog Boxes – Day 1 Built-in Dialog Boxes Input Box Message Box Custom Dialog Boxes – User Forms.
1 MIS309 Database Systems Introduction to Microsoft Access.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
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.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
Making Interactive Programs with Visual Basic .NET
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 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
VBA Programming for Excel Review Excel Objects Excel Methods Identifying Specific Cells Review Functions for Excel Custom Menus.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
How to: Simple User Interface WMCUG 13 th Jan 2010 Gord Vander Vliet Enterprise Tool & Die, LLC Jeff Roark Johnson Controls.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
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.
Excel Tutorial 8 Developing an Excel Application
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
User Forms.
Exploring Microsoft Office Access 2007
VBA Programming for Excel
Presentation transcript:

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming Style Dialog Boxes (UserForm) Worksheet button Functions Spreadsheet vs VBA

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 2 Variable Scope Procedure Level (local) Dim iMarbles As Integer placement: within Sub and End Sub Module Level (global) Dim giTotal As Integer placement: before all Sub statements Project Level (public) Public piGrandTotal As Integer placement: before all Sub statements

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 3 Calling another subprogram Sub FirstOne() MsgBox “In 1st subprogram…” SecondOne End Sub Sub SecondOne() MsgBox “In 2nd subprogram…” End Sub

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 4 Programming Style Good style improves readability of code and your program is easier to maintain! 1. Comments (documentation)  at top (your name, date, etc)  each subprogram  each variable  each major section of code Don’t restate the obvious! Dim iCount As Integer ‘ declare integer variable

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA Good variable names  describe what variables store  not too long, or too short 3. Indentation  subprograms and if statements 4. Constants  for values that do not change 5. White space  leave the occasional blank line Programming Style

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA Minimize variable scope  only make a global or public variable if needed 7. Declare and Initialize variables  Option Explicit forces variable declaration  set initial value of variables appropriately Programming Style

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 7 Programming Style - example

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 8 Programming Style - example

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 9 UserForm Custom dialog box Example:  create UserForm to collect personal info  form will be launched from Excel  data entered in form is stored in Excel

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 10 Insert blank form Insert  UserForm blank UserForm Toolbox with control objects

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 11 Design form Add control objects to form Label (display text) TextBox (for input) CommandButton (perform an action)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 12 Initialize properties Initialize properties of controls Label (Name): lbl… Caption: TextBox (Name): txt… Text: CommandButton (Name): cmd… Caption:

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 13 Write code Determine events to which VBA should respond (eg. clicking Add CommandButton) double-click Add button to open Code window and type the code Private Sub cmdAdd_Click() code goes here... End Sub

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 14 Flowchart Start Name entered? Stop Separate name into first and last Store in worksheet: last, first, phone Clear textboxes on form Report error Phone entered? No Yes

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 15 refers to textbox contents Code

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 16 Code

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 17 Create macro How can we start this form from Excel? There is no macro listed! write a macro to display form form doesn’t appear until called put this code in Module code, not Form code: Sub EnterPersonalInfo ()macro name UserForm1.Show form name End Sub

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 18 Add worksheet button Optional: button on worksheet to run macro >View >Toolbars >Drawing >Oval icon right-click Add Text… right-click Assign Macro… >View >Toolbars >Control >CommandButton icon right-click Properties right-click View Code >Exit Design Mode icon

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 19 Excel Functions Most worksheet functions have a VBA equivalent In VBA: Application.WorkSheetFunction._ FunctionName(arguments) Application.WorksheetFunction.Today() Application.WorksheetFunction.IsNumber (Range(“B3”)) In Excel: =FunctionName(arguments) =Today() =IsNumber(B3)

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 20 Find Function VBA Function – not available on sheet Function is called on an object that defines a spreadsheet range  E.g., Selection.Find(“Tom”) Returns range location of first match, or returns nothing if no match found. Range object need not be sorted

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 21 Macro asks for name to find and stores in E8 Find function locates name, uses offset to get its age vName = InputBox(“Name?”, “Enter name”) vAge = Range("A4:A10").Find(vName). Offset(0, 2).Value Find Function

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 22 VLookUp Function Vlookup (value, table, col_num, close)  value: item to find  table: range of data to search must be sorted by 1 st column  col_num: return data from which column?  close: true or false True: select nearest match – always finds False: find exact, or return #N/A

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 23 How it works in Excel VLookUp Function

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 24 How it works in VBA vName = InputBox(“Name?”, “Enter name”) Range(“E8").Value = vAge vAge = Application.WorksheetFunction. _ VLookup(Range("E8"), Range("A4:C10"), 3, True) Range("F8").Value = vAge VLookUp Function