Vous êtes sur la page 1sur 36

Chapter 1.

Introduction to
VBA
BMGT 302
Business Application
Programming
Scott Hudson

What is VBA
A programming language created by Microsoft to
automate operations in applications
Excel, Access, Word, PowerPoint, and Outlook

No need to purchase VBA separately


A dialect of BASIC (Beginners All-purpose Symbolic
Instruction Code)
Excel exposes its object model to VBA-More on that later!
Macro language
A languages capabilities included the automation of a
sequence of commands in spreadsheet or word-processing
applications

What VBA can do for you


Automating a Recurring Task
Automating a Repetitive Task
Running a Macro Automatically if an Event
Takes Place
Creating your own worksheet functions
Simplifying the Workbooks Look and Feel for
Other Users
Controlling Other Office Applications from Excel

File formats
Excel 2003 and earlier
All excel files ended with .xls extension

Excel 2007 and onwards


Files without VBA code: .xlsx extension
Files with VBA code: .xlsm extension- All
submitted work will have VBA code.
When would you want to send .xlsx files?

Security Settings and Trusted Locations


If macro is disabled, enable as below

Trusted Locations: any .xlsm files in this folder (or its subfolders) will open
without any warning about enabling macros

Warning messages

Accessing the VBA


Environment
Excel 2007

Accessing the VBA Environment


Excel 2013

Accessing the VBA


Environment

Basic terminology
Lines of code
Must obey the rules of syntax
Think of it as an instruction to the computer to do
something

Macro, subroutine, procedure


a set of logically related lines of code that
accomplishes a specific task

Program
all of the subs in an application

Using the Macro Recorder


Recorder memorizes keystrokes and stores them in a Macro

Using the Macro Recorder

Using the Macro Recorder

Running a Macro

Visual Basic Editor


VBE
A user-friendly development
environment where programmed
instructions are maintained

Accessing VBE

VBE Interface

Immediate Window
useful for issuing one-line VBA commands

assuming there is a Data worksheet in the active


workbook

send information to the Immediate window through code


Debug.Print

Understanding Modules
Standard modules
hold macros you create from scratch on your own or from the Macro
Recorder

UserForm modules
belong to a custom user interface object called a UserForm

Class modules
enables you to create your own objects programmatically

Worksheet modules
procedures are tied to various actions called events, such as
selecting a range or entering a value in a cell

Workbook modules
where code is maintained for handling workbook-level events

Object Browser

Working with VBE


Locating your Macros

Understanding the code

Understanding the code


All macros start with a Sub statement
(Sub is short for Subroutine,
commonly referred to as a macro)
that includes the name of the macro,
followed by a pair of parentheses.
Comment lines start with an
apostrophe, are green in color to help
you identify them, and are not
executed as VBA code

Color coding

Editing comments and code

With Statement

Inserting a Module

Renaming a Module

Deleting a Module

Locking and Protecting Your


Code