1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

Similar documents
Working with Macros and VBA in Excel 2007

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Macros in Word & Excel

Excel & Visual Basic for Applications (VBA)

Excel Formatting: Best Practices in Financial Models

Excel 2007 Introduction to Macros

Migrating to Excel 2010 from Excel Excel - Microsoft Office 1 of 1

Excel macros made easy

Excel Programming Tutorial 1

Computer Skills Microsoft Excel Creating Pie & Column Charts

Q&As: Microsoft Excel 2013: Chapter 2

Creating Applications using Excel Macros/Visual Basic for Applications (VBA)

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

ITS Training Class Charts and PivotTables Using Excel 2007

Highline Excel 2016 Class 26: Macro Recorder

Pivot Tables & Pivot Charts

MS WORD 2007 (PC) Macros and Track Changes Please note the latest Macintosh version of MS Word does not have Macros.

Using Flow Control with the HEAD Recorder

Like any function, the UDF can be as simple or as complex as you want. Let's start with an easy one...

Excel 2003: Ringtones Task

Formatting Formatting Tables

Excel Project From the Start menu select New Office Document. If necessary, click the General tab and then double-click Blank workbook.

RIT Installation Instructions

P R E M I E R. Microsoft Excel 2007 VBA (Macros) Premier Training Limited 4 Ravey Street London EC2A 4QP Telephone +44 (0)

Keyboard shortcuts in Excel Keyboard access to the ribbon. In this article WILL MY OLD SHORTCUTS STILL WORK? Hide All

Fig. 1 Visit the Add-Ins dialog to view and load available Add-Ins.

Word 2010: Mail Merge to with Attachments

Programming in Access VBA

Financial Data Access with SQL, Excel & VBA

Excel & Visual Basic for Applications (VBA)

Microsoft Excel 2010 Part 3: Advanced Excel

Advanced Excel Charts : Tables : Pivots : Macros

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

Excel. Microsoft Office s spreadsheet application can be used to track. and analyze numerical data for display on screen or in printed

ECDL. European Computer Driving Licence. Spreadsheet Software BCS ITQ Level 2. Syllabus Version 5.0

Part A: Introduction to Excel VBA

Introduction to Macros and Visual Basic in Excel

EXCEL Tutorial: How to use EXCEL for Graphs and Calculations.

TUTORIAL 4 Building a Navigation Bar with Fireworks

EXAMPLE WITH NO NAME EXAMPLE WITH A NAME

3. (1.0 point) To ungroup worksheets, you can click a sheet of a sheet not in the group. a. index b. panel c. tab d. pane

Introducing VBA Message Boxes

LabVIEW Report Generation Toolkit for Microsoft Office User Guide

Excel 2007 Basic knowledge

Microsoft Office Macros. Presentation Conventions

Excel Pivot Tables. Blue Pecan Computer Training Ltd - Onsite Training Provider :: :: info@bluepecan.co.

Microsoft Migrating to Word 2010 from Word 2003

Excel 2013 Sort: Custom Sorts, Sort Levels, Changing Level & Sorting by Colored Cells

LabVIEW Report Generation Toolkit for Microsoft Office

Basic Microsoft Excel 2007

MS Excel. Handout: Level 2. elearning Department. Copyright 2016 CMS e-learning Department. All Rights Reserved. Page 1 of 11

Intermediate. Microsoft Excel Tables and Printing

MICROSOFT EXCEL Formulas, Functions, & Macros. Documented by Vincent J. Yanusauskas Computer Training Coordinator

Automate tasks with Visual Basic macros

Create Charts in Excel

Fig. 1 The finished UserForm project.

Final Exam Review: VBA

To complete this workbook, you will need the following file:

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

Advanced Microsoft Excel 2010

Mail Merge Creating Mailing Labels 3/23/2011

Microsoft Excel: Pivot Tables

Creating a Project with PSoC Designer

Construction Administrators Work Smart with Excel Programming and Functions. OTEC 2014 Session 78 Robert Henry

To create a histogram, you must organize the data in two columns on the worksheet. These columns must contain the following data:

Creating tables of contents and figures in Word 2013

Introduction. Syntax Statements. Colon : Line Continuation _ Conditions. If Then Else End If 1. block form syntax 2. One-Line syntax. Do...

Microsoft Excel 2010 Tutorial

Smart Connection 9 Element Labels

Training Needs Analysis

Microsoft Excel 2013 Step-by-Step Exercises: PivotTables and PivotCharts: Exercise 1

Adding Comments in Microsoft Excel 2003

Advanced Excel 10/20/2011 1

Page 1 EXPERT. 1.1 Manage Multiple Workbooks Pages Where Covered Modifying existing templates. 356 (Step 7 Tip) Merging multiple workbooks

Import Data to Excel Start Excel.

Windows Phone 7 Mobile Applications Using Decision Structures

Chapter 9 Creating Reports in Excel

WHAT S NEW IN MS EXCEL 2013

Microsoft Migrating to PowerPoint 2010 from PowerPoint 2003

COPYRIGHTED MATERIAL. The Excel macro recorder User-defined functions The Excel Object Model VBA programming concepts

To export data formatted for Avery labels -

Using Loops to Repeat Code

University of Arkansas Libraries ArcGIS Desktop Tutorial. Section 2: Manipulating Display Parameters in ArcMap. Symbolizing Features and Rasters:

Microsoft Office Access 2007 Basics

Creating Basic Excel Formulas

Programming Excel Macros

Excel 2010: Create your first spreadsheet

Notes on Excel Forecasting Tools. Data Table, Scenario Manager, Goal Seek, & Solver

ImageJ Macro Language Quick-notes.

Writing Macros in Microsoft Excel 2003

The Center for Teaching, Learning, & Technology

Add an Audit Trail to your Access Database

Access Tutorial 13: Event-Driven Programming Using Macros

Search help. More on Office.com: images templates

Creating a Gradebook in Excel

Company Setup 401k Tab

As in the example above, a Budget created on the computer typically has:

Using Microsoft Project 2000

Microsoft Excel Basics

Excel Level Two. Introduction. Contents. Exploring Formulas. Entering Formulas

Transcription:

Topics 1 Visual Basic Application Macro Language What You Can Do with VBA macro Types of VBA macro Recording VBA macros Example: MyName () If-Then statement Example: CheckCell () For-Next Loops Example: SquaredSum () Select Case statement Example: CheckCell2() VBA Functions Example: Commission (Sales) Example: Commission3 (Sales, Years) What You Can Do with VBA macro 1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel. Note1: Saving Workbooks with Macro. Note2: Security.

Types of VBA macro 2 VBA Sub Procedure A that either the user or another macro can execute. You can have number of Sub procedures in an Excel workbook. Begins with the keyword Sub and ends with an End Sub Statement. VBA functions A function always returns a (just as a worksheet function always returns a single value). Begins with the keyword Function and ends with an End Function Statement. Recording VBA macros 1. Activate an empty cell. 2. Choose View Macros Record Macro. Excel displays the Record Macro dialog box. 3. Enter a new single-word name for the macro, to replace the default Macro1 name. A good name is MyName. 4. Assign this macro to the shortcut key Ctrl+Shift+N by entering uppercase N in the edit box labeled Shortcut Key. 5. Click OK to close the Record Macro dialog box. 6. Type your name into the selected Cell. 7. The macro is finished, so choose View Macros Stop Recording.

Examining the macro 3 Alt + F11 Testing the macro Alt + F11 View Macros View Macros (Select MyName) Run Editing the macro Alt + F11 Insert the following statements before the End Sub statement. ActiveCell.Font.Bold = True ActiveCell.Columns.AutoFit If-Then statement If condition Then statements [Else else statements] The following example checks the active cell. If it contains a negative value, the font color is changed to red. Otherwise, the color changes to green. Sub CheckCell () If ActiveCell.Value < 0 Then ActiveCell.Font.Color = vbred Else ActiveCell.Font.Color = vbgreen End Sub

For-Next loops 4 For-Next loop is useful to execute one or more statements a number of times. The following example has one statement between the For statement and the Next statement. This single statement is executed 10 times. Sub SquaredSum () Total = 0 For Num = 1 to 10 Total = Total + (Num ^2) Next Num MsgBox Total End Sub The variable Num takes on successive values of 1, 2, 3, and so on, up to 10. The variable Total stores the sum of Num squared, added to the previous value of Total. The result is a value that represents the sum of the first 10 integers squared. This result is displayed in a message box. Select Case statement The Select Case statement is useful for choosing among two or more options.

5 Introduction to Visual Basic Application (VBA Macro Language) In the following example, the active cell is checked. If the value is less than 0, it is colored red. If it s equal to 0, it s colored blue. If the value is greater than 0, it s colored brown. Sub CheckCell2 () Select Case ActiveCell.Value Case Is < 0 ActiveCell.Font.Color = vbred Case 0 ActiveCell.Font.Color = vbblue Case Is > 0 ActiveCell.Font.Color = vbgreen End Select End Sub VBA Functions To create a custom function, follow these steps: 1. Activate the VB Editor (press Alt+F11) 2. Select the workbook in the project window. 3. Choose Insert Module to insert a VBA Module. (Or use an existing Module).

6 Introduction to Visual Basic Application (VBA Macro Language) 4. Enter the keyword Function followed by the function s name and a list of the arguments (if any) in parenthesis. 5. Insert the VBA code that performs the work and make sure that the variable corresponding to the function s name has the appropriate value when the function ends. (This is the value that the function returns.) 6. End the function with an End Function statement. The following function is designed for a sales manager who needs to calculate the commissions earned by the sales force. The commission rate is based on the amount sold those who sell more earn a higher rate. The function returns the commission amount, based on the sales made (which is the function s only argument a required argument). The calculations in this example are based on the following table: Monthly Sales Commission Rate 0 - $9,999 8% $10,000 - $19,999 10.5% $ 20,000 - $ 39,999 12% $ 40,000 + 14%

Function Commission (Sales) 7 Tier1 = 0.08 Tier2 = 0.105 Tier3 = 0.12 Tier4 = 0.14 Select Case Sales Case 0 To 9999.99 Commission = Sales * Tier1 Case 10000 To 19999.99 Commission = Sales * Tier2 Case 20000 To 39999.99 Commission = Sales * Tier3 Case Is > = 40000 Commission = Sales * Tier4 End Select End Function

Function with two arguments 8 The following function increases commission paid by 1 percent for every year that the salesperson has been with the company. For this example, the function has been modified so that it takes two arguments, both of which are required arguments. Call this new function Commission3: Function Commission3 (Sales, Years) Tier1 = 0.08 Tier2 = 0.105 Tier3 = 0.12 Tier4 = 0.14 Select Case Sales Case 0 To 9999.99 Commission3 = Sales * Tier1 Case 10000 To 19999.99 Commission3 = Sales * Tier2 Case 20000 To 39999.99 Commission3 = Sales * Tier3 Case Is > = 40000 Commission3 = Sales * Tier4 End Select

Commission3 = Commission3 + (Commission3 * Years/100) 9 End Function