1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.

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

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.
Using Macros and Visual Basic for Applications (VBA) with Excel
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 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.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Introduction to VBA. What is VBA? VBA stands for Visual Basic for Applications. It is a programming language used exclusively by the Microsoft Office.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Object Variables Visual Basic for Applications 3.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Introduction to Engineering Computing GEEN 1300 Lecture 7 15 June 2010 Review for midterm.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
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.
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’
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.
OCC Network Drives  H:\  P:\ 
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Chapter 9 Macros And Visual Basic For Applications.
ME 142 Engineering Computation I Using Subroutines Effectively.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Visual Basic. The Close Method The Close method is used to close a form. To close a form use the keyword Me to refer to the form. Me.Close()
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Excel Tutorial 8 Developing an Excel Application
Spreadsheet-Based Decision Support Systems
Microsoft Access Illustrated
Microsoft Office Illustrated
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introducing VBA Macros
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
CS285 Introduction - Visual Basic
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal

2 Start the Visual Basic Editor Microsoft office applications (e.g. Excel, powerpoint, word, access) have a built-in visual basic editor Choose menu choice "Tools | Macro | Visual Basic Editor"

3 The Visual Basic Editor

4 Separate Windows The VB editor is opened in a separate window from the Excel spreadsheet. If you close the VB editor you can continue using the spreadsheet. IF YOU CLOSE THE SPREADSHEET, THE VB EDITOR WILL CLOSE Spreadsheet Window VB Editor Window

5 Separate Code Sections There are different code sections. Double click on a sheet or workbook to enter code for that sheet or workbook. Step1: Double click here. Step2: Enter code for sheet1

6 Insert | Module Choose the “insert | module” menu to create a new module. Modules contain generic code that is used by any sheet.

7 Recording Macros

8 Tools | Macro | Record New Macro Choose the menu choice “Tools | Macro | Record New Macro” to start the macro recorder.

9 Enter a name Enter a name for the Macro and press OK

10 Stop Recording Button When you start recording a macro, the “Stop Recording” Toolbar displays. Press the “stop recording” button when you are finished performing the steps for the macro. “Stop Recording” Button

11 Perform the Steps for the Macro in Excel Perform whatever actions you want in Excel. Example: –Step 1: insert a new line at the top of the spreadsheet. –Step 2: click on cell A1 to select it Then press “stop recording”. See next slide for the VBA code that is created by the macro recorder.

12 See code in VB Editor Look at the “Modules” in the VB Editor to see the code for the recorded macro.

13 Execute the Macro To execute the macro, choose the menu choice, “Tools | Macro | Macros”

14 Run the Macro Then choose the macro and click “Run”.

15 The Macro is Executed See the new row

16 VBA vs VB.Net

17 VBA vs VB.Net You can use manyVB.Net concepts and code in VBA Like VB.Net, VBA also provides the ability to create textboxes, buttons, checkboxes, radio buttons, etc. for use in Excel (we will not cover how to do that here). HOWEVER VBA is based on an older version of Visual Basic. There are some differences between how to write VBA and VB.Net code.

18 MsgBox vs MessageBox.Show MsgBox vs MessageBox.show –VBA:MsgBox ("Hello There") –VB.Net:MessageBox.Show("Hello There")

19 No HANDLES clause in VBA Event Handlers –VBA No "Handles" clause. Name of sub determines which event is handled Example Private Sub Button1_Click() MsgBox ("hello there") End Sub –VB.Net Needs "Handles" clause Name of sub does NOT determine which event is handled Example Private Sub Button1_Click( ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click MessageBox.Show("Hello there") End Sub

DIM may not use initialization value VB.Net –In VB.Net you can declare a variable with Dim and give it an initial value all in one step. Dim number as Integer = 1 VBA –In VBA you MAY NOT use an initialization value in a Dim statement. –Therefore the code above would not be legal. –Instead do the following: Dim number as Integer number = 1 20

21 Call Calling a Sub –VBA: Must use the "Call" keyword if sub is in a different module Don't use parentheses if the sub doesn't contain any parameters Example:Call DoIt –VB.Net: Just use the name of the Sub Example:DoIt( )

22 Writing Your Own Code

23 Sub Place all code to be executed in a "subroutine" Will explain more soon... Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub

24 Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub How to run (i.e. execute) a sub To execute the subroutine: –first : place cursor anywhere in the Sub –second : Choose menu choice: Run | Run Macro

25 Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub Other ways to execute a sub To execute the subroutine: –first : place cursor anywhere in the Sub –second : Press F5 OR press the "Run macro" button

26 What happens when you run this subroutine?

27 Running the subroutine Excel spreadsheet displays First message box appears. Press OK Second Message box appears Press OK subroutine is finished (returns to the code window) Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub When you press OK the 2nd message box will appear (not shown)

Functions 28

29 Functions You can define your own VBA functions for use in the Excel spreadsheet.

Sample Function This function calculates the sum of the values from lowNumber to highNumber 30 Function summation(lowNumber As Integer, highNumber As Integer) as Integer Dim count As Integer summation = 0 For count = lowNumber To highNumber summation = summation + count Next count End Function

Using the sample function in Excel 31 Formulas View ValuesView

Rules for Functions The function has a type specified AFTER the parameter list –Function summation(lowNumber As Integer, highNumber As Integer) as Integer The name of the function is used as a variable inside the function The entire function call “becomes the value” that the name of the function had when the function finished executing. 32

33 Quick Intro to VB for the Novice

34 Syntax –syntax means the "grammar" of (or the rules for writing) a programming language or command

35 Syntax of SUB First line : –Must type "Sub" (without the quotes) –make up a name for the sub –must type parentheses Body : –Use VBA statements Last Line: –must type "End Sub" (without the quotes) Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub These are required make up a name for the Sub (more later...) VBA statements go here

36 More than one sub You can have many Subroutines Each subroutine must have a unique name When you execute a subroutine only it runs, not the other subroutines

37 Example Each sub has a unique name You can run each sub separately Sub doSomething ( ) MsgBox ("Hello There") MsgBox ("How are you doing?") End Sub Sub doSomethingElse ( ) MsgBox ("I am having fun.") MsgBox ("Are you having fun?") End Sub

38 Rules for subroutine names Rules for Subroutine names –must start with a letter –can include letters (e.g. A B C a b c etc.) digits (e.g etc.) underscores (i.e. _ ) –may not be a VBA "keyword" some keywords are "sub", "end", "dim" (we'll see more later) –may NOT include any "special" characters (e.g. etc) Examples on next slide...

39 Examples Legal subroutine names –mySubroutine –hello –born2ride –born_to_ride Illegal subroutine names –3cheers may not start with a number –bagels&lox may not include characters other than letters or nubmers or underscore ( _ ) –name with spaces may not include spaces in the name

END OF PRESENTATION The following slides are “in progress” 40

41 MsgBox ("Your message goes here")

42 MsgBox( ) MsgBox ( ) is used to display a dialog box with a message to the user. Syntax: –You must include the word "MsgBox" (without the quotes) followed by a set of parentheses –Place the message in the parentheses –For now (until I teach you otherwise) assume that the entire message should be enclosed in quotes (later we'll learn more about when you don't need the quotes...) –For now (until I teach you otherwise) assume that the entire statement must be typed on one line (later we'll learn how to break up a long line...) Example on next slide...

43 Example Sub showAMessageBox( ) MsgBox ("This will be displayed in a message box") End Sub Put the message in quotes.

44 Numbers If your message only includes a number, you do NOT have to put it in quotes (we'll explain why later) Example: The following is perfectly fine: Sub showANumber( ) MsgBox(123) End Sub No quotes necessary around a number.

45 Expressions

46 Only one "value"

47 String Expression String expressions "This is a constant string expression" "This is another string expression" Numeric expressions 3 3+4

48 msgbox( )

49 Differences between VB.Net and VBA

50 MsgBox vs MessageBox.Show VB.Net: MessageBox.Show(“my message”) VBA: msgbox(“my message”)

51 No HANDLES clause in VBA There is no Handles clause in VBA. In VBA the NAME of the sub indicates which events it handles –Example: button1_click is the name of the click handler for the button named button1