Using Visual Basic for Applications (VBA) Jake Blanchard University of Wisconsin Spring 2010.

Slides:



Advertisements
Similar presentations
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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.
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Writing VBA Macros Record a new Macro, recording only the name, shortcut and description Stop recording Open macro for editing and enter VB code Test.
Visual Basic Introduction Rosalind Archer. 1.1 Running Visual Basic Visual Basic is included with Microsoft Excel. My examples use Excel ‘97. When you.
Visual Basic Rosalind Archer Spring   % +  k  Running Visual Basic Visual Basic is included with Microsoft Excel. My examples use.
VBA Programming Part One. Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: ";
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
CREATE THE DIFFERENCE Before you start Please press F5 to run this show.
Function.
Monte Carlo Simulation in Excel Jake Blanchard Spring 2010 Uncertainty Analysis for Engineers1.
Adding Automated Functionality to Office Applications.
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.
Visual Basic for Applications (VBA) An object-oriented programming language –Instructions for the manipulation of objects –A structured way to provide.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Spreadsheet-Based Decision Support Systems
Languages and Environments Higher Computing Unit 2 – Software Development.
Macros n Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
11 10/10/2005 BAE Spreadsheet Macros (Visual Basic for Applications) Slides to accompany an in-class demo of spreadsheet technique: Macro Programming.
© 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.
ChE 117 Motivation Lots of Tools Can’t always use “Canned Programs”
IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.
Microsoft Excel Macros & Excel Solver (IENG490)
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)
Microsoft Excel Walking Through a Tutorial
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Visual Basic for Applications Macro Programming For Microsoft Office.
Visual Basic for Applications (VBA) An object-oriented programming language –Instructions for the manipulation of objects –A structured way to provide.
15 Aug Visual Basic for Applications Programming language Very similar to Visual Basic Embedded in Microsoft Office applications “Object oriented”
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.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
Engineering Problem Solving with Computers MEPP Summer Residency August 2008.
# 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute.
1 © 2000 John Urrutia. All rights reserved. Qbasic Input, Calculations and Functions.
EIW - ASP Introduction1 Active Server Pages VBScript.
Chapter 9 Macros And Visual Basic For Applications.
Visual Basic I Programming
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
ME 142 Engineering Computation I Using Subroutines Effectively.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
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
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Microsoft Access 2003 Illustrated Complete
Excel VBA Day 3 of 3 Tom Vorves.
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introduction to VBA for Excel and its utilization in numerical methods
Exploring Microsoft Excel
أ.إسراء الطريقي أ. هاله الشملان , 102 تقن , المعمل الخامس
Let’s Learn About Spreadsheets
Visual Basic: Week 5 Review User defined functions
Presentation transcript:

Using Visual Basic for Applications (VBA) Jake Blanchard University of Wisconsin Spring 2010

VBA Macros  Macros allow one to add significant power to Excel  They are small programs that can be called from a spreadsheet  You can create functions or subroutines  If you want to get fancy, you can add a user interface as well

Using Macros  Macros are written in a Basic-like language called Visual Basic for Applications  Excel comes with a separate macro editor  To create a macro, go to Tools/Macro/Visual Basic Editor, then within the Editor go to Insert/Module

You should get this...

Creating a Function Suppose we want to create an Excel function that takes a temperature in Celsius and converts to Fahrenheit We would type the following in a module: Function ctof(temp) ctof = 9 / 5 * temp + 32 End Function

Now we have this...

Using the function  Now go to the spreadsheet and type =ctof(100)  Or, you can put the value of “100” into cell A1 and then type =ctof(A1) into some other cell  In fact, this function can be used just as any built-in Excel function can be used

The Macro Language Operators: +, -, *, /, ^, Mod Comparison: =,, =, <> Logical Operators: And, Eqv, Imp, Not, Or, Xor Intrinsic Functions: Abs, Cos, Sin, Tan, Atn (arc tangent), Exp, Log (natural), Sgn, Sqr (square root), Rnd (random number)

Flow Control If condition Then statements Else statements End If If x=0 Then f=1 Else f=sin(x)/x End If

Flow Control For counter=start To end statements Next For i=1 To 100 sum=sum+i Next

Flow Control Do Until condition statements Loop i=1 x=1 Do Until i=50 x=x*i i=i+1 Loop

Flow Control Do While condition statements Loop i=1 x=1 Do While i<50 x=x*i i=i+1 Loop

Practice Write an Excel function that calculates the sum of cubes of the first N integers Then write an Excel function that calculates the sum of cubes of the first N even integers

My solution Function sumofcubes(N) ans = 0 For i = 1 To N ans = ans + i ^ 3 Next sumofcubes = ans End Function

Another Solution Function moresumofcubes(N) ans = 0 i = 1 Do Until i = N + 1 ans = ans + i ^ 3 i = i + 1 Loop moresumofcubes = ans End Function

Sum of Even Cubes Function sumofevencubes(N) ans = 0 For i = 1 To 2 * N If (i Mod 2) = 0 Then ans = ans + i ^ 3 End If Next sumofevencubes = ans End Function

Creating a Subroutine Subroutines don’t return values...they carry out duties We’ll look at an example

Example Sub Sub writeit() NumPoints = 21 XNot = 0 dX = 0.1 ActiveCell.Value = "X" ActiveCell.Offset(0, 1).Value = ”Sin(X)" x = XNot For i = 1 To NumPoints ActiveCell.Offset(i, 0).Value = x ActiveCell.Offset(i, 1).Value = Sin(x) x = x + dX Next End Sub

Running the Macro  Type the macro into a module in the Visual Basic Editor  Return to a spreadsheet  Create an active cell by clicking into some cell below which you don’t mind Excel writing some data  Go to Tools/Macro/Macros, then click the name of the macro and click Run

Another Way to Run a Macro Go to View/Toolbars/Forms From this Toolbar, click on the button (row 2, column 2) and then trace out a button on a spreadsheet Assign the writeit macro to the button Now click the button

Macro Explanation First 3 lines define constants The next 2 lines write column labels back to the spreadsheet (ActiveCell is the highlighted cell in the spreadsheet) The other lines step through the points, incrementing x and calculating sin(x) The offset writes each result one row below the previous result

Summary and Conclusions VBA is Excel’s macro language Functions return values Subroutines carry out procedures