Visual Basic for Applications IntroductionIntroduction The Control ToolboxThe Control Toolbox View ► Toolboxs ► Control Toolbox Command ButtonsCommand.

Slides:



Advertisements
Similar presentations
ISOM3230 Business Applications Programming
Advertisements

Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
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.
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.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
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
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Using the Visual Basic Editor Visual Basic for Applications 1.
Modeling using VBA. Using Toolbox This is a label This is a button Using the Toolbox select a GUI element and by mouse-click place it on the frame This.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
Microsoft Excel Diane M. Coyle Spring 2009 CS 105.
IE 212: Computational Methods for Industrial Engineering
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.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
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.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
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.
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
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
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Excel Spreadsheet basics. Excel Sheets and Books  Spreadsheet: tool to analyze, chart and manage data for personal, business and financial use Worksheet:
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
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.
Week 2.  Macros revisited  The VBA Editor  The object model  Using variables  If statements.
OCC Network Drives  H:\  P:\ 
Chapter 3 The Visual Basic Editor. Important Features of the VBE Alt-F11 will open the Visual Basic Editor. The Code window is to the right, Project Explorer.
Chapter 9 Macros And Visual Basic For Applications.
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 2 Introduction to VBA (II) ► Lab 1 revisited - Sub & Function procedures - Data types and variable declaration ► Recording.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
ME 142 Engineering Computation I Using Subroutines Effectively.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Excel VBA – Creating Function Procedures Dr Joanna Wyrobek 1.
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.
Developing an Excel Applications
IE 8580 Module 4: DIY Monte Carlo Simulation
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Computer Fundamentals
Microsoft Office Illustrated
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Exploring Microsoft Excel
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CS285 Introduction - Visual Basic
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Visual Basic for Applications IntroductionIntroduction The Control ToolboxThe Control Toolbox View ► Toolboxs ► Control Toolbox Command ButtonsCommand Buttons View ► Toolboxs ► Command Buttons

Visual Basic for Applications Visual Basic Editor (VBE)Visual Basic Editor (VBE) Menu Bar Toolbar Project explorer window Code window Properties window A Simple ProgramA Simple Program Loan Amortization Table Using PMT Function (Lecture 8) Create your first Macro

Visual Basic for Applications Running a ProgramRunning a Program –Press the shortcut key, if there is one. –Select Tools ► Macro ► Macros and click the name of macros in the macro dialogue box. –Press Alt+F8 and click the name of macros in the macro dialogue box. –Click the Run button, ►, press [F5], or go to Run..Run Sub/UserForm to run the program –Create a control bottom (or a command bottom) –Enter the function name and arguments

Visual Basic for Applications Modules and ProceduresModules and Procedures A module is a container for procedures. A procedure is a unit of code enclosed either between the Sub and End Sub statement or between the Function and End Function statements.

Visual Basic for Applications Module FunctionsModule Functions Sub Statement (NB: Subs do not return values, Functions do) –Sub name [(arguments list)] –[statements] –[Exit Sub] –[statements] –End Sub

Visual Basic for Applications Module FunctionsModule Functions Function Statement –Function name [(arglist)] [As type] –[statements] –[name = expression] –[Exit Function] –[statements] –[name = expression] –End Function

Visual Basic for Applications Module FunctionsModule Functions Call Sub Procedure –Call Sub name [(argument list)] Call Function Procedure –Call Function name (argument list) –Object.Value = Function name (argument list) Sometime you have to add Worksheet name or Modules name before the procedure.

Visual Basic for Applications Passing Argument by ReferencePassing Argument by Reference The procedure access to the actual variable in memory. As a result, the variable's value can be changed by the procedure. Passing by reference is the default in VBA.

Visual Basic for Applications Passing Argument by ValuePassing Argument by Value It will pass the value to the corresponding variable, not the memory address.

Visual Basic for Applications Scope of Modules and ProceduresScope of Modules and Procedures Procedures in Visual Basic can have either private or public scope. Procedure with private scope A procedure with private scope is only accessible to the other procedures in the same module

Visual Basic for Applications Scope of Modules and ProceduresScope of Modules and Procedures Procedure with public scope A procedure with public scope is accessible to all procedures in in every module in the workbook in which the procedure is declared, and in all workbooks that contain a reference to that workbook.

Visual Basic for Applications Objects and CollectionsObjects and Collections Objects are the fundamental building blocks of Visual Basic. Objects are the fundamental building blocks of Visual Basic. An object is a special type of variable that contains both data and codes. An object is a special type of variable that contains both data and codes. A collection is a group of objects of the same class. A collection is a group of objects of the same class.

Visual Basic for Applications Objects and CollectionsObjects and Collections The most used Excel objects in VBA programming are Workbook, Worksheet, Sheet, and Range. The most used Excel objects in VBA programming are Workbook, Worksheet, Sheet, and Range. Workbooks is a collection of all Workbook objects. Workbooks is a collection of all Workbook objects. Worksheets is a collection of Worksheet objects. Worksheets is a collection of Worksheet objects.

Visual Basic for Applications Objects and CollectionsObjects and Collections The Workbook object represents a workbook. The Workbook object represents a workbook. The Worksheet object represents a worksheet. The Worksheet object represents a worksheet. The Sheet object represents a worksheet or chartsheet. The Sheet object represents a worksheet or chartsheet. The Range object represents a range of cells The Range object represents a range of cells

Visual Basic for Applications Workbook and Worksheet ObjectWorkbook and Worksheet Object A workbook is the same as an Excel file. A workbook is the same as an Excel file. The Workbook collection contains all the workbooks that are currently opened. The Workbook collection contains all the workbooks that are currently opened. Inside of a workbook contains at least one worksheet. Inside of a workbook contains at least one worksheet.

Visual Basic for Applications Workbook and Worksheet ObjectWorkbook and Worksheet Object In VBA, a worksheet can be referenced as followed: Worksheets("Sheet1") In VBA, a worksheet can be referenced as followed: Worksheets("Sheet1") Worksheets("Sheet1") is the worksheet that named "Sheet1." Worksheets("Sheet1") is the worksheet that named "Sheet1." Another way to refer to a worksheet is to use number index like the following: Worksheets(1) The above refers to the first worksheet in the collection. Another way to refer to a worksheet is to use number index like the following: Worksheets(1) The above refers to the first worksheet in the collection.

Visual Basic for Applications Workbook and Worksheet ObjectWorkbook and Worksheet Object To refer sheets (or other objects) with the same name, you have to qualify the object. For example: Workbooks("Book1").Worksheets("Sheet1") Workbooks("Book2").Worksheets("Sheet1")

Visual Basic for Applications Range Object and Cells PropertyRange Object and Cells Property Range represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range

Visual Basic for Applications Range Object and Cells PropertyRange Object and Cells Property Range object has a Cells property. Range object has a Cells property. The Cells property takes one or two indexes as its parameters The Cells property takes one or two indexes as its parameters Cells(index) or Cells(row, column)

Visual Basic for Applications Range Object and Cells PropertyRange Object and Cells Property ActiveSheet.Range.Cells(1,1) Range.Cells(1,1) Cells(1,1) ActiveSheet.Range.Cells(1,1) Range.Cells(1,1) Cells(1,1) Range("A1") = 123 and Cells(1,1) = 123 Range("A1") = 123 and Cells(1,1) = 123 Cells(12) = "XYZ" puts "XYZ" on Cells(1,12) or Range("L1") assume cell A1 is the current cell Cells(12) = "XYZ" puts "XYZ" on Cells(1,12) or Range("L1") assume cell A1 is the current cell Range("B1:F5").cells(12) = "ZYZ" Range("B1:F5").cells(12) = "ZYZ"

Visual Basic for Applications Methods and PropertiesMethods and Properties A Property represents a built-in or user-defined characteristic of the object. A Property represents a built-in or user-defined characteristic of the object. A method is an action that you perform with an object. Below are examples of a method and a property for the Workbook Object. A method is an action that you perform with an object. Below are examples of a method and a property for the Workbook Object.

Visual Basic for Applications Methods and PropertiesMethods and Properties –Workbooks.Close Close method close the active workbook –Workbooks.Count Count property returns the number of workbooks that are currently opened

Visual Basic for Applications Methods and PropertiesMethods and Properties Methods That Take No Arguments: Worksheets("Sheet").Column("A:B").AutoFit Methods That Take No Arguments: Worksheets("Sheet").Column("A:B").AutoFit Methods That Take Arguments: Worksheets("Sheet1").Range("A1:A10").Sort _ Worksheets("Sheet1").Range("A1") Methods That Take Arguments: Worksheets("Sheet1").Range("A1:A10").Sort _ Worksheets("Sheet1").Range("A1")

Visual Basic for Applications Methods and PropertiesMethods and Properties

Visual Basic for Applications VariablesVariables Dim x as Type Data Types –Integer (2 bytes) –Long (long integer, 4 bytes) –Single (single-precision floating-point, 4 bytes) –Double (double-precision floating-point, 8 bytes) –Currency (scaled integer, 8 bytes) –Decimal (14 bytes) –Date (8 bytes) –String (variable-length, 10 bytes) Array(arglist)Array(arglist)

Visual Basic for Applications Array(arglist)Array(arglist) Before signing values to an array, the array needs to be created. You can declare the array by using the Dim statement. Dim Array_name(Begin to End, Begin to End)

Visual Basic for Applications CommentsComments ‘ This is a comment, not part of the program

Visual Basic for Applications Input and Output (I/O)Input and Output (I/O)Input –From Cells Set x = Range( “ A1 ” )Set x = Range( “ A1 ” ) –From Screen x = InputBox(Prompt:="Please enter a number.")x = InputBox(Prompt:="Please enter a number.")Output –To Cells Range( “ A1 ” ) = xRange( “ A1 ” ) = x –To Screen The Message BoxThe Message Box MsgBox “ The value of x is ” & Str(x)

Visual Basic for Applications Arithmetic OperatorsArithmetic Operators Power/Exponent ^ Operator Multiplication * Operator Division / Operator Addition + Operator Subtraction - Operator

Visual Basic for Applications Concatenation OperatorsConcatenation Operators Concatenate Strings & Operator (String)

Visual Basic for Applications Type Conversion FunctionsType Conversion Functions Change expression to Currency CCur(expression) Change expression to Date CDate(expression) Change expression to Double CDbl(expression) Change expression to Decimal CDec(expression)

Visual Basic for Applications Type Conversion FunctionsType Conversion Functions Change expression to Integer CInt(expression) Change expression to Long CLng(expression) Change expression to Single CSng(expression) Change expression to String CStr(expression)

Visual Basic for Applications Type Conversion FunctionsType Conversion Functions Change text to number VALUE(text) Change value to text TEXT(value,format_text) Change number to String STR(number)

Visual Basic for Applications Control FunctionsControl Functions If, Then If expression Then statements [Else [elsestatements]] End If

Visual Basic for Applications Control FunctionsControl Functions Do, Until Do Until x = y statements statementsLoop

Visual Basic for Applications Control FunctionsControl Functions Do, While Do While x = y statements statementsLoop

Visual Basic for Applications Control FunctionsControl Functions For, Next For counter = start To end [Step step] [statements] [statements] [Exit For] [statements] [statements] Next [counter]

Visual Basic for Applications Logical FunctionsLogical Functions Logical AND AND(logical1,logical2,...) Logical OROR(logical1,logical2,...)

Visual Basic for Applications Mathematical FunctionsMathematical Functions Count COUNT(value1,value2,...) Natural LogarithmLN(number) Maximum of a SeriesMAX(number1,number2,...) Minimum of a SeriesMIN(number1,number2,...) Power/ExponentPOWER(number,power) Product of a Series PRODUCT(number1,number2,...) RoundROUND(number,num_digits) Sum of a SeriesSUM(number1,number2,...)

Visual Basic for Applications Statistical FunctionsStatistical Functions Correlation CoefficientCORREL(array1,array2) CovarianceCOVAR(array1,array2) KurtosisKURT(number1,number2,...) Lognormal Distribution LOGNORMDIST(x,mean,standard_dev) MeanAVERAGE(number1,number2,...) MedianMEDIAN(number1,number2,...) ModeMODE(number1,number2,...) Normal Distribution NORMDIST(x,mean,standard_dev,cumulative)

Visual Basic for Applications Statistical FunctionsStatistical Functions Percentile RankPERCENTRANK(array,x,significance) PercentilePERCENTILE(array,k) QuartileQUARTILE(array,quart) RankRANK(number,ref,order) SkewnessSKEW(number1,number2,...) SlopeSLOPE(known_y's,known_x's) Standard DeviationSTDEV(number1,number2,...) Standard Normal DistributionNORMSDIST(z) VarianceVAR(number1,number2,...)

Visual Basic for Applications Financial FunctionsFinancial Functions Depreciation DDB(cost, salvage, life, period[, factor]) Depreciation, Straight LineSLN(cost, salvage, life) Depreciation, Sum-Of-Years' DigitsSYD(cost, salvage,life, period) Future Value FV(rate, nper, pmt[, pv[, type]]) Interest PaymentIPmt(rate, per, nper, pv[, fv[, type]]) Interest RateRate(nper, pmt, pv[, fv[, type[, guess]]])

Visual Basic for Applications Financial FunctionsFinancial Functions Internal Rate of ReturnIRR(values()[, guess]) Modified Internal Rate of ReturnMIRR(values(), finance_rate, reinvest_rate) Net Present ValueNPV(rate, values()) Number of Periods (Annuity)NPer(rate, pmt, pv[, fv[, type]]) Present ValuePV(rate, nper, pmt[, fv[, type]]) Principle PaymentPPmt(rate, per, nper, pv[, fv[, type]])