Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.

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.
Essence of programming  Branching  Repetitions.
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.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
© 2002 ComputerPREP, Inc. All rights reserved. Excel 2000: Customizing Excel and Using Macros.
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
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Using the Visual Basic Editor Visual Basic for Applications 1.
Adding Automated Functionality to Office Applications.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
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.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
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.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
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.
Enhancing User Interaction Through Programming
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.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Lecture Excel: Macros & Pivot Tables. Macros A macro is a series of commands that are stored and can be run whenever you need to perform the task.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Applications Macro Programming For Microsoft Office.
Excel VBA TOM VORVES DO THE ACTIVITY EXERCISE ON PAGE 53 IF YOU WISH ElaineNikky JennArrinMylinh Matt Move Row.
OCC Network Drives  H:\  P:\ 
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.
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.
Access Lesson 11 Creating and Running Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
Working with Date Variables (Unit 5)
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Microsoft Excel Prepared by the Academic Faculty Members of IT.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
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
After completing this lesson, you will be able to:
Microsoft Access Illustrated
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Microsoft Excel 2003 Illustrated Complete
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Microsoft Office Excel 2003
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009

Lesson 1 Objectives Record a Macro Modules Examine the VBE window –Project Explorer, Object Browser, Properties Sub Procedures –Write a Sub procedure Examine statement code –Define objects properties –Edit code

Personal Macro Workbook A hidden workbook that is always open Stores global macros –Save in the Personal Macro Workbook Use the View tab to unhide the Personal Workbook

Visual Basic Editor Project VBA Project Properties Sub Procedure in Module 1 Module

Sub Procedures Sub Enter_Text() ‘ Enters the text Commission in selected cell Range (“E1”).Select Active Cell.FormulaR1C1=“Commission” End Sub KeywordProcedure Name (arguments, optional) Comment Statements

Record a Macro Display the Developer Tab Click on the Macro tool Proceed through steps to complete the macro

Stop Recording Tool Click on the Stop Recording Tool

Edit a Macro Open the Visual Basic Editor Make the changes needed Save and Close the VBE

Absolute Cell References

Relative Cell References

Debug Open the VBE and Set a Breakpoint Start running the procedure Step through the code

Debug continued Open the VBE and Set a Breakpoint Start running the procedure Step through the code Bellerephon Investment Advisors Stock Prices Symbol: Open: High: Low: Close: Net Chg:Pct Chg:Port Alloc:Pct Prt Chg: BPAH $ $ $ 9.23 $ $ (2.18)-17%10%-2% CHJD $ $ $ $ $ %4%0% CPDH $ $ $ $ $ (2.74)-13%8%-1% EOWH $ $ $ $ $ (2.87)-12%7%-1% OBAH $ $ $ $ $ (0.69)-5%5%0% PWHG $ $ $ $ $ %5%1% QLHZ $ $ $ $ $ 1.305% 0% QUJG $ $ $ $ $ %10%3% SDJB $ $ $ $ (0.49)-1%5%0% SJCK $ $ $ $ $ (5.11)-16%4%-1% SJGL $ $ $ $ $ %8%2% SLHW $ $ $ $ (6.50)-13%3%0% WKCH $ 1.56 $ 2.34 $ 1.94 $ 2.13 $ %10%4% WQOC $ $ $ $ $ 1.007%5%0% ZXLQ $ 4.05 $ 6.43 $ 3.56 $ 5.25 $ %6%2%

Debug continued Procedure InsertRowsCols –Correction Rows(“1:4”) Procedure InsertTxt –Corrections A1 Procedure FmtTxt –Correction Columns(“H:J”) … Percent –Move EntireColumn.AutoFit above End Sub

Adding a Tool to the Quick Access Toolbar Click on the Customize button on the Quick Access Toolbar Select Macros

Adding a Tool to the Quick Access Toolbar continued Click on Create Report Click Add

Adding a Tool to the Quick Access Toolbar continued Click on Create Report Click Add and OK

Customize the New Tool Click on the Customize menu Select the Macro Click on the Modify button Change the Tool image Change the Tool name

Lesson 2 Objectives Objects – Range –Selection –Active Sheet Methods –Select Properties –CurrentRegion –Sort –Name

Objects, Methods, & Properties Objects have properties that govern their appearance and behavior –Name of worksheet Methods govern the action the objects perform –Protect a worksheet Properties govern how the objects respond to a user action –Procedure is executed when the event occurs

Variables A variable is a name given to hold data. When a procedure uses a variable the current ‘value’ is used

Option Explicit and Dim Statements Using Option Explicit in a module helps maintain the integrity of each variable. Dim statements are a way to define variables in a procedure Ex. Dim NumberOfEmployees As Integer

Loops For Next Loops If Then If Then Else Case Do Loops –Do While –Do Until

For Next Sub ForNext Loop () For Counter = 1 to 10 [Step 1] Code is written here Next Counter – continues to collect iterations as a count End Sub

If…Then Syntax

If…Then…Else Syntax

Use a Do Until…Loop Sub DoUntilLoop () Do Until ActiveCell = “” Code is written here Loop End Sub

Case Is… Syntax

Find first blank row at the bottom of a list Open the file Our Company Create a Sub procedure named Database Sub GoToBottom() ‘ Goes to the first blank row in an Excel list ActiveSheet.Cells(Rows.Count,1).End(xlUp).Offset(1).Select End Sub

Offset and Count Properties ActiveCell.Offset(3,2) (row,column) Rows.Count The number of active rows in a worksheet Cells(Rows.Count,1) 1 st column of the last row of the list End(xlUp.Offset(1) The cell below.. The first blank row in a list

Lesson 3 Objectives Dialog box Input box Message box Command buttons

Dialog box and User Input

SortBy Procedure

DateTime Procedure RepSort Procedure

Lesson 4 Objectives Insert, Copy and Delete Worksheets Rename worksheets Change worksheet order Print worksheets

More Methods Add Method –Worksheets.Add Delete Method –Worksheets(2).Delete Copy Method –Worksheets(2).Copy After:=Worksheets(2)

Name Property

More Methods PrintPreview –Worksheets(2).PrintPreview PrintOut –Worksheets(2).Printout –Worksheets(“SE Sales”).PrintOut

Unit 5 Objectives Create a User Defined Function (UDF) –Declare a variable –Perform a calculation

User Defined Functions Begin with the keyword Function –The first line of code includes the function name as well as any arguments –Arguments are enclosed in parentheses and separated by commas –The code between the first and last lines perform the mathematical calculation –The last line of code must return the calculated answer to the name of the function End with the keywords End Function

Function Commission(Sales) If Sales >= Then pct = 0.15 ElseIf Sales >= Then pct = 0.12 ElseIf Sales >= Then pct = 0.1 ElseIf Sales >= Then pct = 0.08 Else pct = 0.05 End If Commission = Sales * pct End Function

PriceLevel Function

Automate Sum Functions

Wishing you a Wonderful Holiday Season