0% found this document useful (0 votes)
28 views59 pages

Excel VBAExcel 2003

This document provides an introduction to using Visual Basic for Applications (VBA) in Excel. It discusses the differences between macros and VBA, how to record and run macros, and how to insert and remove modules. It then covers VBA basics like variables, conditional statements, loops, procedures, functions, arrays, and debugging tools. The goal is to explain the fundamentals of programming in VBA within Excel.

Uploaded by

Juan Jose Gomez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views59 pages

Excel VBAExcel 2003

This document provides an introduction to using Visual Basic for Applications (VBA) in Excel. It discusses the differences between macros and VBA, how to record and run macros, and how to insert and remove modules. It then covers VBA basics like variables, conditional statements, loops, procedures, functions, arrays, and debugging tools. The goal is to explain the fundamentals of programming in VBA within Excel.

Uploaded by

Juan Jose Gomez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Excel VBA

Introduction

Prepared By
Daniel Lamarche
ComboProjects
Excel VBA
Last Update: 21 June 2011

Introduction
Prepared by Daniel Lamarche
[email protected]

Table of contents
INTRODUCTION .................................................................................................................................... 4
DIFFERENCE WITH MACROS AND VBA ..................................................................................................... 4
WHAT DO I DO WITH VBA? .................................................................................................................. 5
Recording a new Macro................................................................................................................ 5
Where are all my macros?............................................................................................................ 6
Inserting a new module ................................................................................................................ 6
About projects and modules ........................................................................................................ 6
Removing a module...................................................................................................................... 7
About the Personal macro workbook .......................................................................................... 7
WANT TO KNOW MORE ABOUT VBA? .................................................................................................... 8
Where should I start? ................................................................................................................... 8
MY FIRST MACRO................................................................................................................................. 9
Recording the MyReport macro ................................................................................................... 9
Running the Macro ....................................................................................................................... 9
Viewing the macro ....................................................................................................................... 9
About The Object Model ............................................................................................................ 11
PART 1 - INTRODUCTION TO VBA.......................................................................................................... 12
Sub Procedures........................................................................................................................... 12
Functions .................................................................................................................................... 12
VARIABLES ......................................................................................................................................... 13
Forcing the declaration of variables ........................................................................................... 14
Reasons to declare all variables ................................................................................................. 14
Scope of variables....................................................................................................................... 14
THE IMMEDIATE WINDOW ................................................................................................................... 15
Date - Time functions ................................................................................................................. 16
Working with numbers ............................................................................................................... 16
String functions .......................................................................................................................... 16
Concatenation ............................................................................................................................ 17
Excel objects ............................................................................................................................... 17
Useful keyboard shortcuts in the Immediate Window .............................................................. 19
Useful keyboard shortcuts in the Code Window ....................................................................... 19
Maybe the most common use of the Immediate Window ........................................................ 19
COMMENTING YOUR CODE .................................................................................................................. 20
INDENTATION..................................................................................................................................... 21
THE CONTINUATION CHARACTER........................................................................................................... 22
CONDITIONAL STRUCTURES .................................................................................................................. 23
The If – End If structure .............................................................................................................. 23
The Select Case structure ........................................................................................................... 24
Important difference between the If and the Select Case structure ......................................... 25
Nesting If – End If statements .................................................................................................... 25
LOOPING STRUCTURES......................................................................................................................... 26
The Do – Loop structure ............................................................................................................. 26
The For – Next structure ............................................................................................................ 28
Nested For Loop ......................................................................................................................... 29
The For Each – Next flavour ....................................................................................................... 29
PASSING PARAMETERS ........................................................................................................................ 30
Supplying a parameter to a function .......................................................................................... 30
Supplying a parameter to a Sub ................................................................................................. 31
NAMED PARAMETERS.......................................................................................................................... 32
THE WITH STATEMENT ........................................................................................................................ 33
Nested With Statements ............................................................................................................ 34
DEBUGGING YOUR CODE ..................................................................................................................... 34
The Debug.Print statement ........................................................................................................ 35
Using Breakpoints to stop the code ........................................................................................... 35
The Current Line of Execution .................................................................................................... 35
About the Immediate Window................................................................................................... 36
Adding a Watch .......................................................................................................................... 36
ERROR HANDLING............................................................................................................................... 37
The On Error Statement ............................................................................................................. 37
The Resume Next Statement...................................................................................................... 38
UNDERSTANDING ARRAYS .................................................................................................................... 39
About arrays and index .............................................................................................................. 39
Looping through an array ........................................................................................................... 39
Declaring Arrays ......................................................................................................................... 40
What is the size of an array? ...................................................................................................... 41
Dumping an array in a worksheet .............................................................................................. 41
About dynamic arrays ................................................................................................................ 42
About the Redim statement ....................................................................................................... 42
Two dimensional arrays ............................................................................................................. 42
In conclusion............................................................................................................................... 43
PART 2 - MORE ABOUT EXCEL’S OBJECT MODEL...................................................................................... 44
About The Term Object .............................................................................................................. 44
About The Term Collection......................................................................................................... 45
About Properties and Methods .................................................................................................. 46
Specifying an Item in a Collection .............................................................................................. 47
Looping Through Items in a Collection ....................................................................................... 48
ENTERING FORMULAS IN A RANGE......................................................................................................... 49
The R1C1 Notation ..................................................................................................................... 49
EXPLORING THE RANGE OBJECT ............................................................................................................. 51
Referencing a Range ................................................................................................................... 51
The CurrentRegion property ...................................................................................................... 52
The Columns and Rows Properties............................................................................................. 52
The Resize Property .................................................................................................................... 53
The Offset property .................................................................................................................... 54
The Cells property ...................................................................................................................... 54
Multiple Ranges Selections ........................................................................................................ 56
IN CONCLUSION ................................................................................................................................. 57
CONTINUE YOUR EXPLORATION ............................................................................................................ 58
Excel VBA Introduction – Part 1

Introduction
Excel VBA1 is a programming language used to automate Excel actions. You probably have heard that
this was the job of macros. Although it is associated with programming the word macro is often
misinterpreted and should be used sparingly.
The term ‘Macro’ comes from an archaic language used in the 80’s to group many commands into one
macro (literally 'large') command. Microsoft has perpetuated a major confusion with the users by still
referring to them as "macros" instead of VBA procedures.
Running a macro command would run all the application’s commands associated with this macro. For
instance a PrintReport macro would:
1) Import Data from an external file
2) Eliminate extraneous columns
3) Bold column headings
4) Add subtotals at the bottom and other summary
5) Print the report
All these tasks would be carefully planned and rehearsed by the developer then recorded (or written)
using the ‘Macro Language’ native to the application. All these tasks would then be grouped under the
name PrintReport.
When the user would run the PrintReport macro, the application would execute each command
starting with the first one down to the last one. Finally the macro would terminate.

Difference with Macros and VBA


Macros have evolved in many ways since their inception. The need for custom solutions has forced
companies to develop more complex programming structures that were absent from the early macro
language.
Why? Because macros were designed to replay actions stored within them, full stop. The need for
alternate actions and looping through multiple values arose quickly. As well it became crucial for
developer to control to control an external application (one example would be sending emails with
Outlook using email addresses in Excel) The following scenario shows an example of tasks that were
very difficult to do and manage with early macros:
If the value in the State cell is “Victoria” then scan all the Sales, gather all values in Victoria and
calculate the sum, the average and the count of these values then copy the result in another
sheet. Continue this process with all other states until there are no more cells to examine.
With time more structured languages appeared with many more constructs and statements allowing
data manipulation. Microsoft chose to use the BASIC language and turn it into a much more flexible
language called Visual Basic for Applications (VBA). This language was integrated throughout the Office
Suite for over a decade.
Some basic features of VBA not found in early macro language are:

1
This document uses the Excel 2003 command interface. When appropriate a note will indicate the
Excel 2007 alternative.
4
Excel VBA Introduction – Part 1

If - Then - Else ............. Allow alternate statements to be run based on the result of a condition.
Select Case .................. Executes one of several groups of statements, depending on the value of one
expression.
Do While - Loop .......... Execute one of several groups of statements as long as a condition remains
true.
For - Next .................... Run one or several groups of statements a specified number of times.
For Each – Next ........... Run one or several groups of statements on each element (object) in a
collection of objects. For example all Worksheets in a Workbook.
With – End With.......... The With statement allows you to perform a series of statements on a specified
object without qualifying the name of the object over again.
VBA is also much more procedural in the sense that existing code can be reused; something that was
technically inexistent in the macro language.
This mean that if, for example, you create a command that alerted the user about a situation, you
could built it in such a way that the same statements could be used for a variety of situations by using
the same procedure over and over again.
In conclusion, you can create macros that will repeat what you taught them to do or you can create
new tools that don’t exist in Excel and use them to intelligently automate your everyday tasks

What Do I Do With VBA?


If you find yourself repeating a complex task then these steps can be recorded in a macro then rerun
as many times as needed. In this course we will learn how to record a few macros and assign them to
buttons in a toolbar for easy access.
Recording a new Macro
To record a new macro choose Tools > Macro > Record New Macro. In the Record Macro dialog box
type the name of the macro and specify where the macro will be saved. The name of the macro cannot
contain spaces and cannot start with a number. You can
optionally type a description. For example, alongside the name of
the macro is FormatReport2.
Click OK once everything is fine and manually execute every step you want to record. When all the
steps are completed click the Stop Recording button. Instead of using the Tools menu all the time,
display the Visual Basic toolbar. To do this, right-click on any button and select Visual Basic to display
the toolbar.
In Excel 2007 you need to use the Developer's tab. This tab is not enabled
by default. Choose Office Button > Options and enable the Show
Developer tab in the Ribbon option in the Popular menu on the left. In the
Code group you will find the buttons allowing you to create new macros as
well as to run existing macros.

2
In this document we will use the mixed case notation to name all VBA objects. In the mixed case
notation all characters are in lowercase except for the first letter of each word.
5
Excel VBA Introduction – Part 1

The Web site www.comboprojects.com contains a link to a guided project using the macro recorder. In
the Handout link click the Excel VBA Files in the Excel 2003 section or Excel 2007 to download a zip file
contain a PDF file, three workbooks and two text file. The tutorial in the PDF file will give you a solid
basis in what VBA is all about.
Where are all my macros?
All macros are saved in modules inside the workbook where they were created. They exist in a parallel
environment called VBE (Visual Basic Editor).
These modules can only be seen using the VBE. To access the VBE press Alt+F11 on your keyboard. In
the Project window you see the names of all the open workbooks. Each workbook has a Microsoft
Excel Objects item that you can expand or collapse. If you have created a macro in a workbook, it will
also have a Module item. Below is a screenshot of the VBE with the name of its major component.

Split Bar
Project window
Ctrl+R Code window
F7

Properties window
F4

Immediate window
Ctrl+G

Inserting a new module


To insert a new module, select the workbook in which you want to add a module (below it is Book1)
and choose Insert > Module. You can also right-click the project name and choose Insert > Module.
About projects and modules
On the right you can see a typical Project window. Every workbook has an
associated VBA Project. Here it is named VBAProject. In parenthesis, next
to the project name is the workbook name.
For the same reason you save a workbook under a different name than
Book1, you might want to rename your project under a more user-friendly
name.
To rename your project:
 Right-click on VBAProject name and choose VBAProject Properties. If the Project window is not
visible press Ctrl+R.
 In the Project Name box type your project name. The name cannot contain spaces so you need to
use the mixed case notation. For example: ‘MyProject’ or ‘UsefulFunctions’.
 Click OK.
6
Excel VBA Introduction – Part 1

Modules are a little bit like worksheets. Normally you organise


your work in different worksheets in a way that makes sense to
you. Additionally you rename sheets using names that help you
to remember its content.
The same as Sheet1 is not a very clear name for a worksheet, Module1 is not very descriptive for a
module. To rename a module you need to view the Properties window.
 Click Module1 in the Project window (or whatever the number is).
 In the Properties window double-click the Name property. If the Properties Window is not visible
hit F4.
 Type a new name for the Name property and hit Enter. This will become the name of the module.
The name cannot contain space so you should use mixed case here too. Examples of names are
‘basFinancialFunctions’ or ‘basDatabaseTools’.
Beginners in Excel VBA need not to rely on the Properties window that much. When you learn about
UserForms though, it will become indispensable. For the moment use the Property window only to
give all your modules a descriptive name.
Most developers start module names with the prefix bas so that it does not conflict with the names of
your Subs or Functions in any module in your workbook.
Removing a module
The Project window is useful to manage the project’s name and the modules. Eventually you will want
to remove a module from the list. To remove a module:
 Right-click on the module name.
 Choose Remove [ModuleName]. You will be asked if you want to export the code in the module
before removing it.
 Click No. If you click Yes Excel will export the code in your module to a text file with a .bas
extension.
About the Personal macro workbook
When you record a macro you can store it in the Personal macro
workbook. Doing this will make your macro accessible to all
workbooks. This workbook not different from any other
workbooks except that it has a ‘magic’ name: PERSONAL.XLS and
it is loaded every time you open Excel.
To quickly way create a ‘global’ macro workbook, follow these
simple steps:
 Click Record Macro. Accept the default name.
 Select Personal Macro Workbook in the Store Macro in drop down list.
 Click OK then click Stop Recording. Excel has created the PERSONAL workbook.
The location of this workbook is special. It is normally located in the XLSTART folder. All workbooks in
the XLSTART folder are automatically open when Excel is launched. The XLSTART folder can be found in
the following path:
C:\Documents and Settings\[your login name]\Application Data\Microsoft\Excel\XLSTART

7
Excel VBA Introduction – Part 1

Depending on your Windows setup, you may also find it under the Program Files folder:
C:\Program Files\Microsoft Office\Office11\XLSTART
In Windows 7 the folder is C:\Users\AppData\Roaming\Microsoft\Excel\XLSTART
The PERSONAL workbook is not visible in the Excel interface. It will be visible in VBE though. To see the
PERSONAL workbook in Excel, choose Window > Unhide. In the Unhide dialog box select
PERSONAL.XLS and click OK.3 If you followed the steps above to create the PERSONAL macro workbook
you may want to delete its Module1 since it only contains a dummy macro.
It is a good idea to keep this macro workbook hidden because it will be in the way every time you open
Excel and you (or a user) run the chance of modifying it accidentally. To hide the PERSONAL.XLS
workbook make it the active workbook then choose Window > Hide.
If you make any change to the PERSONAL.XLS macro workbook you will be asked to save the changes
upon exiting Excel.

Want To Know More About VBA?


Visual Basic for Applications is a programming language. In other words you type statements and
structures using VBA statements. Fortunately Excel makes it very easy to learn VBA by recording all
your actions and create the VBA code that you can examine and learn from.
An interesting side to VBA is that once you have a good understanding of the various Visual Basic
statements in Excel you can transport that knowledge in other applications. This means that the VB
(Visual Basic) part of VBA is the same in Access, Word, PowerPoint, AutoCAD and many more. The A
part (Applications) describes the Object Model of that application. The Object Model is what takes
time to learn.
To illustrate think of a person that mastered mechanics. This person understands perfectly well the
concepts behind motors and the relationship between petrol, pistons and spark plugs as well as the
role electricity play in the overall technology.
Now a friend asks him to take a look into his power generator. Although many of the principles learned
can be applied to a generator our mechanic faces a very different kind of device. With a sound basis in
mechanics and electricity (Visual Basic) he will get familiar with a power generator (a different object
model) fairly quickly.
Where should I start?
To learn VBA in Excel you need to learn about Visual Basic first. As you progress, you will learn to
integrate Excel’s object in your code. This course will give you a good foundation on which you can
learn much more about VBA and Excel.
 This first part will be about macros in Excel and their limitation.
 A good introduction about programming in VBA will be provided.
 Exploring the object model in Excel and integrating it with some VBA code.

3
In Excel 2007 the workbook name is PERSONAL.XLSB. You can toggle its visibility using the Unhide
and Hide commands in the View tab then the Window group.
8
Excel VBA Introduction – Part 1

My First Macro
In this section we are going to use the Macro Recorder to generate the VBA code for a small report.
Then we are going to look at the code and amend it so that it looks better and runs smoother.
It is important to remember that there are many things the Macro Recorder cannot record. For
instance it knows nothing about variables, how to use IF structures and DO loops. On the other hand
the Macro Recorder will show you some basic VBA code allowing you to access and manipulate Excel
objects.
Recording the MyReport macro
In Excel 2003 display the Visual Basic toolbar. The first two buttons
allow you to run and record macro respectively. In Excel 2007 click
the Developer tab to find the corresponding commands.
Follow these steps:
1. While in a new workbook click Record Macro.
2. In the Macro Name box type ‘MyReport’.
3. In the Shortcut key box type an uppercase ‘M’. This will assign the Ctrl+Shift+M keystroke to the
macro. Click OK.
4. Click cell A1 (even if are already in A1) and type your company name and hit Ctrl+Enter.
5. Select A1:D1 and click Merge and Center.
6. Increase the font size of cell A1 to 16 pts.
7. In A2 type ‘Prepared by’ and type your name.
8. In A4 type ‘Travel’ and in A5 type ‘Meals’.
9. In A6 type ‘Total’ and hit Arrow Right.
10. In B6 type '=SUM(B4:B5)' and hit Ctrl+Enter.
11. Format B4:B6 as 'Currency'
12. Format A5:B5 with a Bottom Border.
13. Select cell B4
14. Click Stop Recording.
Running the Macro
Move to a blank worksheet and press Ctrl+Shift+M. A report similar
to the one on the right appears. Type a value in B4 and another
one in B5 to complete the report.
One problem with this sort of macro is that they will always
reproduce the same report. What if, for example, we wanted
another report using another name in cell A2. Using the Macro Recorder this simple task would be
impossible. If you require different names in the report then you will need to create one macro for
each name! Not practical.
Viewing the macro
To view the code recorded by the macro display the VBE window by hitting Alt+F11.
If the Project window is not visible press Ctrl+R. Expand the Module branch and double-click Module1
to open it in the Code window. If necessary maximize your Code window.
9
Excel VBA Introduction – Part 1

Scroll down to the bottom of the module and


briefly look at the code generated by the Macro
Recorder. There are over 60 lines of code in this
procedure! On the right is the same procedure
when we will be done amending the code.
Can you think of a couple of reasons why this is
not very efficient?
 Difficult to read and understand.
 Lots of useless lines of code that are not even
used.
 Runs slowly.
 Most instructions that are in multiple lines
can use one line instead.
To amend the code we are going to use the
keyboard shortcut Ctrl+Y (Delete a line of code).
We also want to merge multiple line instructions
into one. For example in the lines:
Range("A1").Select
Selection.FormulaR1C1 = "ComboProjects"
The object Selection refers to cell A1 selected in the previous line. Also the reference to the
FormulaR1C1 property is a bit over the top. It is more efficient to write instead:
Range("A1").Value = "ComboProjects"
This situation appears about a dozen times in the code. Another example is:
Range("A6").Select
ActiveCell.FormulaR1C1 = "Total"
Needs to look as follow:
Range("A6").Value = "Total"
Finally we will group together related properties that relate to the same object using the With
statement. It appears a couple of times in the code but we can do even better. The With statement
allows you to specify many things to be done with the same object. Here’s a dummy example:
With The CurrentWorksheet
.Rename it
.Move it
.Change all column with to a value
.Protect it
End With
As you can see above, the four instructions between the With and End With will all run on the
dummy object called here CurrentWorksheet. You can add as many instructions as required
preceded with a full stop (called a dot).

10
Excel VBA Introduction – Part 1

About The Object Model


You should not be intimidated by the expression Object Model. Maybe you are not familiar with the
word Object. Here's a simplified definition of the word object with regard to programming: Object is a
glorified word for a thing in whatever application you’re in. In Excel for example an object can be a
Workbook, a Worksheet, a Range, a Chart or a single Cell.
The same as objects can do things and have attributes used to describe them, objects in Excel use
methods to do tasks and they can be described with properties. All of these objects are arranged in a
hierarchy that is known as the Excel object model.

VBA communicates with Excel’s object model to access different objects in a Workbook. In other
words Excel exposes its object hierarchy to VBA through its object model. Since all objects in a
workbook (including Excel itself) are tied to this model they can be entirely manipulated from VBA.
To see how VBA knows about Excel's objects choose Tools > References while in Excel's VBE. The
second item that is checked should be Microsoft Excel 11.0 Object Library. This library contains the
information about where each object is in the hierarchy of Excel objects.
For example a worksheet has a Name property that allows you to read or change its name. A cell has
Font and ColorIndex properties allowing you to examine or change the font and colour for that cell.
The first time you save the workbook you use the SaveAs method of the Workbooks object to specify
a name. Then you can use the Save method to update your changes or the Close method to close the
workbook.
As you can see, properties and methods names often resemble Excel commands but it is not always
the case. You will need to learn a new vocabulary to be able to talk to Excel through its object model.
This takes a while.
With what we have learned above can you guess if the last word of each of these statements is a
method or a property? Ask yourself if the last word sounds like an action (Method) or an attribute
(Property).
Worksheets.Add
Charts("Sales2007").HasTitle
Sheets.Count
Range("A1:C3").Select
Range("C1:C5").Copy
ActiveCell.CurrentRegion.Address

11
Excel VBA Introduction – Part 1

Part 1 - Introduction to VBA


At the very basic of all VBA projects are Procedures and Functions. The following are simple definitions
of those two important building blocks:
Sub Procedures
A Sub procedure (or simply a Sub) contains code that will do some actions in your applications. All
recorded macros in Excel are sub procedures. The general look of a sub is as follow:
Sub ProcedureName(Argument As DataType)
<VBA code to run>
End Sub
The argument is an optional parameter that the procedure will use in its code. This is an ideal way to
create reusable code. The procedure is then used for many possibilities. Obviously you can use more
than one argument for a sub. The argument could be the name of the worksheet in which the code will
be executed or a cell’s address where you need to apply some format features.
Functions
A function contains code that will return a single value. Think of a function as a question. Your custom
functions appear in the ‘User Defined’ category of the Insert Function command. In the following
image you can see one function listed in the User Defined category. Only custom functions appear in
this list.

What do we mean by return a single value? Returning here


means that once the function has executed all the code within
itself, it will send back a value to be used by more code. You may
also want that returned value to appear in a cell.
On the right you see a function called NextMonday and under
the list it is specified that it uses one argument: A date. This
function returns the date of the Monday following any valid date.
You can use this function like any other functions in Excel. For example if cell A1 contains the date 10-
Nov-2007 then typing in any cell:
=NextMonday(A1)
This will return 12-Nov-2007 (The first Monday after the 10-Nov-2007). This function could also be
used inside a procedure. Again this is an ideal way to create reusable code. Here’s a fake example of a
Sub using the NextMonday function:
Sub SendInvoice()
If NextMonday(Today) - Today < 3 Then
<run this code>
Else
<run this code instead>
End If
End Sub
This procedure will run some code if the following Monday is less than three days away otherwise it
will run some other code. To be able to do this, NextMonday() must be a function and return a value. It
this case it returns the number of days until the following Monday.
12
Excel VBA Introduction – Part 1

The general look of a function is as follow:


Function FunctionName (Argument As DataType) As DataType
<VBA code to run>
FunctionName = SomeValue
End Function
Two important elements of a Function are:
 The end of the Function declaration (the first line) specifies the data type returned by the function.
For example a function could return a number, a string or a boolean (True/False).
 Near the end of the function, the function name is assigned a value. This is where you specify the
value to be returned by the function. In the example above the function name is FunctionName
and the last statement assign the value SomeValue to be returned by the function.

Variables
A variable is an object represented by a single word that will hold a value while the procedure (Sub or
Function) is running. They lose their value once the procedure is over. Sounds simple? It is.
It is exactly like the good old time when your math teacher said “OK let x equal 10.” So for the duration
of the problem x = 10. Once the problem was solved ‘x’ disappeared from the board. It became out of
scope or nonexistent.
In VBA you need to create a variable and assign it a data type before you assign a value to it. This is
because a variable can refer to a whole number, a decimal number, a text string, an array of values,
even a whole workbook!
Creating a variable is called declaring a variable. When you declare a variable you also specify its data
type. When you become more familiar with VBA in Excel (or in any applications) you will fully
appreciate why you need to declare all variables.
To declare a variable use the Dim statement with As to specify the data type. Here are a couple of
examples:
Dim intQuantity As Integer ' A whole number
Dim strAnswer As String ' Some text
Dim sngDiscount As Single ' A number with decimals
Dim wksJanuary As Worksheet ' Do we need to explain?
Dim rngTable As Range ' A selection in a sheet
The first three variables will simply hold data: A whole number, some characters and a decimal
number. The last two are Object Variables designed to hold objects in the Excel object model: A
Worksheet and a Range object. Once a variable is declared you can assign a value (or an object) to that
variable.
Here are some easy examples:
intQuantity = 20
strAnswer = "VBA is fun to learn"
sngDiscount = 0.05
Set wksJanuary = Worksheets("January")
Set rngTable = Range("A1:C5")

13
Excel VBA Introduction – Part 1

Once initialised, you can use a variable’s value simply by using its name. You can even change its value
to suit the current need.
You will note that variables assigned to objects (as opposed to data) need to use the Set statement
otherwise you will get an error and your procedure will stop.
Forcing the declaration of variables
By default VBA does not enforce variable declaration; it is an option that you can set. Do yourself a
favour and enable this option once and for all by following these steps:
 In VBE choose Tools > Options
 In the Editor tab enable Require Variable Declaration.
In the future, all new modules you create will have the following line in the Declaration section (at the
top):
Option Explicit
Reasons to declare all variables
Here are a couple of reasons why all variables should be declared in all procedures:
1. It saves memory usage. The amount of memory reserved for a variable will correspond to its data
type.
2. Once declared you can use an amazing feature called Complete Word that will automatically
complete the variable names for you. To use Complete Word simply type the first three or four
characters and press Ctrl+Space.
3. If you type a variable name and make a typo you will get a “Variable Not Defined” error as soon as
you attempt to execute the code and VBA will highlight the unknown variable. This is because,
being a typo, this variable was not declared. So you should take advantage of the Complete Word
feature discussed above.
4. With Object variables you will able to benefit from the AutoList feature. For example typing
ActiveSheet and a period will not activate the AutoList feature. This is because there are different
types of sheets in Excel. It could be a normal worksheet or a chart sheet.
Declaring a variable as Worksheet object and assigning it to the ActiveSheet will display the
AutoList associated to a worksheet.
5. Finally VBA will often warn you if you accidentally assign the wrong type of value to a variable. For
example you cannot assign a text string to a numeric variable unless the string is exclusively made
of numeric digits. This becomes even more important with object variables.
Most experienced programmer will tell you that if a language allows variable declaration then by all
means you should use this feature.
Scope of variables
The scope of a variable determines how a variable can be accessed and how it can be used by the
procedures.
The most common type of scope is the procedure scope. This simply means that all variables declared
within a procedure can only be accessed by the statements within the procedure where they are
created.
Sub MyProcedure()
Dim Value As Integer
14
Excel VBA Introduction – Part 1

Dim Message As String


Value = 12
Message = "The quantity is"
Debug.Print message & " " & Value
End Sub
In the procedure above the variables Value and Message can only by perceived within
MyProcedure. Once the procedure is done executing all its statements, the variables are out of
scope and thus don’t exist anymore.
Another type of scope is Module Scope. A module scope variable is declared at the very top of the
module, before any procedure in the module (the declaration section of the module.)
This approach will allow you to assign a value (or an object) to the variable without declaring it in every
procedure in the module.
Dim Value As Integer
Sub MyProcedure()
Value = 12
Debug.Print Value * 4
End Sub
Function MyFunction(Message As String) As String
Message = Message & " " & Value
MyFunction = Message
End Sub
Here the variable Value is declared once for both procedures in that module. All procedures can
assign a value to the variable. Once a procedure is over the variable retains its value and other
procedures in the same module can access and change its value.

The Immediate Window


One of the first windows you will learn to appreciate when programming in VBA is the Immediate
Window (sometime called the Debug Window.) It is the tool par excellence to learn about VBA
functions, make experiences with objects and variables, master concatenation and observe the
evolution of variables in your code. Without the Immediate Window programming in VBA would not
be the same!
To display and move the focus in the Immediate Window, press Ctrl+G while in VBE. In this section we
will see a few things to do the Immediate Window so that you learn more about VBA and Excel object
model. The examples we will see in this section (except for the ones referring to Excel’s object model)
also apply to other Microsoft Office applications.
 Create a new workbook containing three worksheets (Sheet1 to 3) and press Alt+F11 to display the
VBE.
 Press Ctrl+G to move to the Immediate Window.
Type the following expressions and hit Enter after each one. Write your own observations next to each
line so that you can remember what it does. You do not need to respect the capitalisation. You can
type everything in lowercase but mixed case makes it easier to read.

15
Excel VBA Introduction – Part 1

To learn more about a statement move the insertion point in the VBA function name and hit the F1 key
(Help). Sometimes the Help feature contains a short example containing code. You can copy this
example and paste it in a module to try it.
Date - Time functions
?Date
?Time
?Now
?Month(Date)
?MonthName(Month(Date))
?Weekday(Date)
?WeekdayName(2)
?DateAdd("d",15,Date)
?DateDiff("ww",#09/15/2007#,Date)
(VBA dates must be entered as month/day/year)
?Format(Date,"dddd dd-mmm-yy")
?Format(Date,"mmmm-yy")
?DateSerial(Year(date),Month(Date)+1,1)
Working with numbers
?FormatPercent(0.05,1)
?FormatCurrency(1200,0)
?Abs(-5)
?Sqr(144)
?Fix(3.14159)
?25 Mod 5 also try ?25 Mod 6
?25 \ 6
?Sgn(-2) also try ?Sgn(2)
?Cint("3.6")
?IsNumeric("12") try with "Day"
?Round(17.268,2) try with 1 or 0

String functions
x="Learning VBA can be fun"
?x
?Len(x)
?Left(x,8)
?Right(x,3)
?Mid(x,10,3)
?InStr(1,x,"can")
16
Excel VBA Introduction – Part 1

?Replace(x,"VBA","Piano")
?LCase(left(x,8))
?UCase(Right(x,3))
?InStr(1,”Know-it-All","-") try 5 an 8
?Trim(" Wow ")
?StrConv("VISUAL BASIC",vbProperCase)
?StrConv("VISUAL BASIC",vbLowerCase)
?Choose(2,"Mouse","Screen","Printer")
?LTrim(" Sky ") try with RTrim
?String(15,"-")
?CurDir
Concatenation
a=<replace with your first name>
b=<replace with your surname>
c=<replace with your suburb>
?a & b & c
?a & " " & b & " lives in " & c
?UCase(b) & " " & Left(a,1) & "."
d = UCase(b) & " " & Left(a,1) & "."
?d
?d & "(" & c & ")"
Excel objects
Choose Insert > Module to create a new module in your workbook. In the module type the following
lines:
Sub LearningExcel()
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
End Sub
While the insertion point is inside the procedure, press F8 on your keyboard twice. Type lines below in
the Immediate Window and hit Enter after each one. Note the result for each on the right. Some lines
will not display a value in the Immediate Window.
You might need to press Alt+F11 to toggle between Excel and VBE to see the result or view Excel and
the VBE window simultaneously.
Set wkb = ActiveWorkbook
?wkb.Name
?wkb.Sheets.Count
wkb.Worksheets.Add
17
Excel VBA Introduction – Part 1

ActiveSheet.Move After:=Sheets(Sheets.Count)
?wkb.Saved (That is Saved)
wkb.SaveAs("MyWorkbook")4
?wkb.Saved
?wkb.Name
?wkb.FullName
wkb.Worksheets("Sheet1").Activate
wkb.Sheets("Sheet1").Range("A1")="Hello"
Set wks=wkb.Sheets("Sheet2")
?wks.Name
wks.Name = "January"
wks.Activate
wks.Range("A1")="January Sales"
wks.Range("A1").Font.Bold=True
Set rng = wkb.Sheets(wks.name).Range("A2:C2")
rng.Interior.ColorIndex=37
rng.Cells(1,1)="Name"
rng.Cells(1,2)="Qty"
rng.Cells(1,3)="Amount"
set rng = rng.Offset(1,0)
rng.Cells(1,1)="Bob"
rng.Cells(1,2)=5
rng.Cells(1,3).Formula="=B3*45"
set rng = rng.Offset(1,0)
rng.Cells(1,1)="Dan"
rng.Cells(1,2)=7
rng.Cells(1,3).FormulaR1C1="=RC[-1]*45"
set rng = rng.Offset(1,0)
rng.Cells(1,1)="Total"
rng.Range(Cells(1,2),Cells(1,3)).Select
Selection.FormulaR1C1="=SUM(R[-2]C:R[-1]C)"
wkb.Save

4
In Excel 2007 you need to use wkb.SaveAs "MyWorkbook",52. This number forces the
workbook to be saved as a Macro Enabled Workbook (.xlsm).
18
Excel VBA Introduction – Part 1

Click in the code window and press F5 to terminate the procedure. This will annihilate the three
variables.
Useful keyboard shortcuts in the Immediate Window
In the Immediate Window there are a few keyboard shortcuts that you should know about. Some also
work in the Code Window as well. Do not attempt to learn all these shortcuts in one day. Keep them
close and adopt them one at a time.

Key Immediate Window Code Window


Enter Execute current line Insert a new line and honour indentation
Ctrl+Enter Insert a new line n/a
Ctrl+Space Complete Word Complete Word
Ctrl+Backspace Delete word to the left Delete word to the left
Ctrl+ or  Move insertion point one word Move insertion point one word
F1 Display Help about the current statement Display Help about the current statement
F7 Move focus to Code Window n/a
Ctrl+A Select All Select All
Ctrl+I Display Screen tip Display Screen tip
Ctrl+J AutoList AutoList
Ctrl+N Insert new line Insert new line
Ctrl+Y Delete current line (Cut) Delete current line (Cut)

Useful keyboard shortcuts in the Code Window


The Code Window also has a few keyboard shortcuts that will make things run smoother.
Key Code Window
F2 Display the Object Browser
F4 Open the Properties Window
F5 Run current Sub if not arguments are required
F8 Step into a procedure in break mode
F9 Insert a break point at current line
Tab, Shift+Tab Increase, decrease indentation (Backspace also decrease indentation)
Ctrl+F Launch the Find command
Ctrl+G Move Focus to Immediate Window
Ctrl+R Open the Project Window
Ctrl+S Save all modules (the current workbook)
Alt+F11 Toggle between the application and VBE
Ctrl+Break (Pause) Cause an endless loop to stop
Ctrl+ or  Move one procedure up or down

Keyboard shortcuts will allow you to focus on the task you want to do and you will be doing it faster.
Do not attempt to learn all these shortcuts in one day. Keep them close and adopt them one at a time.
Keyboard shortcuts will allow you to focus on the task you want to do and you will be doing it faster.
Maybe the most common use of the Immediate Window
Since the Immediate Window is also known as the Debug Window (it used to be called that way) it
must also be useful for debugging. For instance you can print a variable value in the Immediate
Window by using the Debug.Print statement anywhere in your code.
19
Excel VBA Introduction – Part 1

The Debug.Print statement will print the value of a variable so that you can see if it is what you
would expect it to be. For example a variable can change value many times in a loop. The
Debug.Print statement followed with the variable name will allow you to keep a temporary record
of the value. For instance in the Code Window type the following simple procedure:
Sub AsciiChar()
Dim i As Integer
For i = 1 To 10
Debug.Print i + 64; Chr(i + 64)
Next i
End Sub
This not-too-useful procedure will print in the Immediate Window the first 10 letters of the alphabet
along with their ASCII value. For example A is ASCII 65, B is ASCII 66 and so on.
Make sure the Immediate Window is opened
and empty (Press Ctrl+A then Delete). While
the insertion point is anywhere in the
procedure hit F8. The first line is highlighted.
The procedure runs in Break Mode. This means that each line will be executed only when you hit F8.
Hit F8 two times. You are now in the line with the Debug.Print statement. Press F8 once and look
in the Immediate Window. You see the number 65 along with the letter A.
Continue pressing F8 and the procedure will execute the For loop again. The second
time the loop is executed the values 66 and the letter B are printed in the Immediate
Window.
Reset
Continue pressing F8 until the loop is executed 10 times or click Reset in the
Standard toolbar to stop the execution of the procedure.
There are a few other techniques that can be used to debug your procedures. We will get familiar with
some of them later in this document.

Commenting Your Code


One critical aspect of programming is adding comments inside your code to document its use.
Comments are invaluable when you revisit your code at a later time. To add a comment anywhere in a
module, start by typing an apostrophe then the text for your comment. By default comments appear
in green font and are ignored by VBA so that they are not executed.
If you have a hard time appreciating the word Commenting then try Documenting and you should see
how important this is. Use comments to document the functionality of your code and the various
progressive updates. For example you may type a date and a short description of the change.
The next example shows that comments can use a whole line or be added at the end of a line.
Sub AsciiChar()
' Prints the first 10 ASCII chars.
Dim i As Integer
For i = 1 To 10
Debug.Print i + 64; Chr(i + 64) ' A is character 65.
Next i ' Move on to the next number.

20
Excel VBA Introduction – Part 1

End Sub
Use comment to document updates and revisions to your code. At the top of the module you could,
for example, briefly describe each procedure in the module with an example of how to use it. An
interesting use of comments is to stop a line from being executed. You might want to test a procedure
without running a few lines but you don’t want to delete them.
VBE has an Edit toolbar with two buttons Comment Block and Uncomment Block allowing you to
comment and uncomment multiple lines. Choose View > Toolbars > Edit to make the toolbar visible.

Indentation
Indentation has nothing to do with code execution but all to do with readability. As you might expect
you increase indentation with the Tab key and decrease it using Shift+Tab or Backspace.
In VBA many constructs have a begin statement and an end statement. All lines of code inside such
constructs should (maybe must) be indented. By default the Tab is set to four spaces.
The following is a list of the most common constructs that use the Begin and End pair of statements.

Sub – End Sub With – End With


Function – End Function Do While – Loop
If – End If For – Next
Select Case – End Select

In the following (incomplete) example of a Sub procedure you can see how indentation allows you to
see where each pair of statements starts and ends (don’t worry about the code. Consider the
indentation instead):
Sub SelectACell()
Dim myRange As Range
On Error Resume Next
Set myRange = Application.InputBox("Select a cell please.")
If Not myRange Is Nothing Then
Debug.Print myRange.Address(0, 0)
Range(myRange.Address).Select
Else
Debug.Print "Cancelled by user."
End If
End Sub
Can you see how the If-Else-End If structure is easy to locate visually? Imagine if there was
more constructs of that type embedded inside one another.
Now look at the same as the one above but without indentation. Can you see how difficult it is to
decipher its structure?
Sub SelectACell()
Dim myRange As Range
On Error Resume Next
Set myRange = Application.InputBox("Select a cell please.")
If Not myRange Is Nothing Then
Debug.Print myRange.Address(0, 0)
21
Excel VBA Introduction – Part 1

Range(myRange.Address).Select
Else
Debug.Print "Cancelled by user."
End If
End Sub
One of the nice features of VBE is indenting multiple lines in one operation. Select a few consecutive
lines and hit Tab to indent or Shift+Tab (do not use Backspace with multiple lines) to decrease
indentation of the selection.
The procedure above uses only three levels but it is not uncommon to see much more than that. The
next example uses fake code to show even more levels of indentation:
Sub ShowIndentation ()
' This fake example illustrate well the use
' of indentation in code.
If Value > 100 Then
If Weight < 25 then
Do While Duration < 12
Update each value
Loop
Else
Select Case Weight
Case 1 to 15
Message = "To high!"
Case 16 to 24
Message = "Careful Now!"
End Select
End IF
Else
Value = Value * 1.1
End If
End Sub

The Continuation Character


When a line of code is very long you can use the continuation character to split it in two or more lines.
The continuation character is the underscore “_”. It must be preceded by a space and must be the last
character in a line.
In the following example, at the end of the first line, there is a space and an underscore. It simply tells
VBA that the current line continues on the next line as if it was one very long line.
Set myRange = Application.InputBox("Select a cell please.", _
"Where", ActiveCell.Address(False, False), Type:= 8)
This is very useful for at least two reasons:
 Since VBE will not word wrap your code, you don’t need to scroll to the right to see the end of long
lines of code.
 If you print your code the printer will wrap your code when it decides that the line hits the right
margin. This will most certainly make your printout hard to read.

22
Excel VBA Introduction – Part 1

The continuation character gives you the possibility to break a line where you want. Your printouts will
therefore be easier to read. In the next example a long concatenation is broken in multiple lines to
make it easier to read (and print):
strMessage = "Do you really want to delete customer " _
& varCustomer & " from your list?" & vbLf _
& "This customer has " & varCount & " pending items to ship."
Normally this line would too long for your screen. Breaking it in shorter lines makes it easier to read
and print. The Visual Basic constant vbLf stands for Line Feed. The message is broken in two lines.
Normally the continuation character is added after a closing talking marks or after a VBA constant.
Another good place is after a comma used for separating parameters. The two examples above use the
continuation character after a comma, a closing talking mark and a VBA constant.
The following example is invalid:
strMessage = "Do you really want to _
delete this customer?"

Conditional Structures
Of all the control structures available in VBA, conditional structures may be the most common ones.
There are basically two different conditional structures:
 If – End If
 Select Case – End Select
Both of them allow you to execute some lines of VBA code depending on the result of some condition.
Let’s see a few examples of If – End If structures.
The If – End If structure
The following straightforward If – End If statements will execute some code if the variable intValue
is greater than or equal to 10.
If intValue >= 10 Then
<VBA code to run if True>
End If
Note that if the value is less than 10 no code is executed in this If – End If. For some alternative code
add the Else statement to the structure. In this case we should call it an If – Else – End If structure.
If intValue >= 10 Then
<VBA code to run if True>
Else
<VBA code to run if False>
End If
In the example above if the number is less than 10 then the statements following the Else keyword
will be executed.
If you have more than two conditions you can use the ElseIf key word. For example:
If Performance = 1 Then
Bonus = Salary * 0.1
ElseIf Performance = 2 Then

23
Excel VBA Introduction – Part 1

Bonus = Salary * 0.09


ElseIf Performance = 3 Then
Bonus = Salary * 0.07
Else
Bonus = 0
End If
The example above computes a bonus based on job classification. The statement following the Else
statement will be executed only if all the conditions above are False.
The condition statement can contain And as well as Or to compute more complex conditions.
Consider the following If statements:
If Value >= 10 And Value <=20 Then
If City = "Sydney" Or City = "Adelaide" Then
The first one will return True if the value is between 10 and 20. The second one will return True if the
city is either Sydney or Adelaide.
This last example will return True if the city is either Sydney or Adelaide and the amount is above 1000.
If (City = "Sydney" Or City = "Adelaide") And Amount > 1000 Then
The Select Case structure
The Select Case – End Select is very useful when there are many possibilities for the same
expression. This is how a Select Case basically works:
Select Case <variable>
Case <first possibility>
<VBA code to run>
Case <next possibility>
<VBA code to run>
Case Else
<VBA code to run>
End Select
The code following the Case Else statement will run if all the Case statements above fail. Note that
each Case statement can examine one or more possibilities. Here are some various ways to use a
Select Case structures:
Select Case Model
Case "P101" ' If variable Model = P101
intSurcharge = 100
Case "P201", "P202" ' If variable Model is P201 or P202
intSurcharge = 150
Case Else ' All other possibilities
intSurcharge = 200
End Select
The code above guaranties that the variable intSurcharge will be assigned a value.
When you have only one statement per case you can further simplify it by typing the statement on the
same line as the Case statement. The example above could be written like the following (note the use
of the colon):

24
Excel VBA Introduction – Part 1

Select Case Model


Case "P101": intSurcharge = 100
Case "P201", "P202": intSurcharge = 150
Case Else: intSurcharge = 200
End Select
Each Case can contain more complex statements. Look at the following example:
Select Case performance
Case 1 ' Performance is 1
Bonus = salary * 0.1
Case 2, 3 ' Performance is 2 or 3
Bonus = salary * 0.09
Case 4 To 6 ' Performance is 4 to 6
Bonus = salary * 0.07
Case Is > 7 ' Performance is above 7
Bonus = 100
Case Else ' All other values
Bonus = 0
End Select
The Case Else statement will run when performance is equal to what values?
Important difference between the If and the Select Case structure
Did you note an important difference between the two structures discussed in this section? If not you
could be puzzled with a problem for a while.
While it seems that the Select Case is more intuitive and straightforward you should keep in mind that
the Case statements will be comparing several possibilities against one expression.
On the other hand you can create an elaborate If structure that will compare multiple expressions. In
this example the conditions use three different variables. You cannot do this with a Select Case.
If Var1 > 10 And Var1 < 20 Then
<VBA code to run>
ElseIf Var2 = "P201" Then
<VBA code to run>
ElseIf Var3 <> "Brisbane" Then
<VBA code to run>
Else
<When all else fails>
End If
Nesting If – End If statements
It is common to nest an If – End If statement inside another. This is where indentation becomes
really important. Without indentation it would be very difficult to understand its structure (or levels)
and how the code runs. For example:
If Suburb = "Geelong" Then
If Qty < 20 Then
Discount = 0.06
Else
25
Excel VBA Introduction – Part 1

Discount = 0.10
End If
Else
Discount = 0.04
End If
If the variable Suburb is equals to Geelong then there is an additional test to do: Check if the quantity
is below 20 (for a discount of 6%) or 20 or more (for a discount of 10%). If the suburb is not Geelong
then the discount is 4%.

Looping Structures
The purpose of a loop is to get VBA to repeat a snippet of code a certain number of times. The
iteration can be specified as a fixed number (e.g. do this 10 times), or variable (e.g. do this for as many
times as there are rows of data).
Loops can be constructed in many different ways to suit different circumstances. Often the same result
can be obtained in different ways to suit your personal preferences. The examples below will
demonstrate various ways to use loops in VBA.
There are two basic kinds of loops, both of which are demonstrated below: Do - Loop and For -
Next loops. The code to be repeated is placed between these keywords. Since looping is very often
used in programming it is suggested that you get quickly familiar with the different looping structures
in VBA.
The Do – Loop structure
In a Do - Loop structure the code between the Do and the Loop statements is executed for as long
as the condition specified in the Do line remains True. This type of loop is the one to use when you
cannot predict when the condition will become false. In other words, you don’t know how many times
the loop will iterate. Here’s a dummy example:
Do While <there is a value in the cell to the left>
valAmount = valAmount + the value to the left
Move one cell down
Loop
When the code enters the loop starting with the Do statement, it checks if the condition on the right of
the Do evaluates to True. If there is indeed a value in the cell to the left of the active cell then the code
in the loop is run. It increments the value of valAmount with the value found in the cell then move
the active cell down one row and repeat the loop again.
If the condition is still True the second time the loop is executed and the value of valAmount is
increased by the value of the next cell found. Now valAmount is equal to the sum of the first two
cells. The process continues until the condition next to the Do statement evaluates to False, there is no
value in the cell to the left. Data Val
The table on the right shows a loop executed five times. The first time val will equal 5. 5 5
The next time it is equal to 5+8=13, then 13+3=16, then 16+12=28 and so on until there 8 13
is not data in column Data. When the code runs the active cell is at the top of the 3 16
second column. 12 28
The actual code for such a procedure is: 9 37

26
Excel VBA Introduction – Part 1

Sub LoopCummul()
Dim valAmount As Integer
' As long as the cell to the left is not empty...
Do While Not IsEmpty(ActiveCell.Offset(0, -1))
' Cummulate values and write total in active cell.
valAmount = valAmount + ActiveCell.Offset(0, -1)
ActiveCell.Value = valAmount
' Move down one cell by selecting it.
ActiveCell.Offset(1, 0).Select
Loop
End Sub
This is a perfectly valid macro and it can be run anywhere in a worksheet. The macro will start
summing all values to the left until there are no more. If the cell to the left the active cell is blank VBA
will exit the loop. As you can easily appreciate, imagine writing a macro that would cumulate 1,000
values without using a loop!
The Do – Loop structure provides a mean to exit the loop if an unpredictable condition occurs. You
might decide that if the value to the right is a word then the loop must stop and the Sub must stop. To
exit a loop use the Exit Do statement and the code will continue after the Loop statement. For
instance:
Do While Everything is Fine
If Something Goes Wrong
Exit Do ' Resume after the Loop statement.
Else
Run the code
End If
Loop
Depending on how you interpret the condition (and personal preferences) there is also another flavour
you can use: The Do Until – Loop.
In a Do Until – Loop iteration will occur as long as the condition is False. The loop stops when it
becomes True. We could illustrate the two variations in plain English using:
Do While <The condition is True> ' Run while it is true
<VBA code to run>
Loop
Or
Do Until <The condition becomes True> ' Run while it is false
<VBA code to run>
Loop
Think of the Do Until flavour like a Do While except that the condition needs to be is False
instead of True for the loop to continue. Here is the same code found at the beginning of this section
but written another way:
Do Until <there is no more value in the cell to the left>
valAmount = valAmount + the value to the left
Move one cell down

27
Excel VBA Introduction – Part 1

Loop
It takes a little while to become familiar with various loops. Practice a bit by typing examples and you
will be fine.
The For – Next structure
If you know (or can get VBA to find out) how many times to repeat a block of code you can use a For
- Next loop. For example it is very easy to find out how many worksheets are in the current
workbook.
The For – Next loop requires a variable that will increment for each iteration until it reaches the
maximum value specified in the For line. When the variable reaches this value the loop automatically
stops and the code continues after the Next statement. Here’s a dummy example:
Dim x As Integer, valAmount As integer
For x = 1 to 10
valAmount = valAmount + x
Debug valAmount ' Print valAmount in the Immediate Window
Next x
The first time the loop is executed x=1 so valAmount = 1. The second time x=2 so valAmount = 3
(1+2), then when x=3, valAmount = 6 (3+3) and so on until the loop is executed ten times. The
variable valAmount will then be equal to 55: The sum of the first ten numbers. Here is an example of
a function using a For – Next loop:
Function CountHyphen(strWord As String) As Integer
Dim i As Integer, intCount As Integer
For i = 1 To Len(strWord)
If Mid(strWord, i, 1) = "-" Then
intCount = intCount + 1
End If
Next i
CountHyphen = intCount
End Function
You could use this function in Excel to count how many hyphens are in a cell entry. For example if A1
contained ‘The co-worker was well-received’ then the formula =CountHyphen(A1) would return 2.
The For – Next structure provides a mean to exit the loop if an unpredictable condition occurs. To
exit a loop use the Exit For statement and the code will continue after the Next statement.
One last note about the For – Next loop. You can change the way the variable increments by
specifying a value for the Step statement in the For line. In the next example the loop will run 10
times:
For i = 0 to 18 Step 2
<VBA code to run>
Next i
The variable i will have the value 0, 2, 4, 6, 8 and so on to 18. In the following example the variable
decrements by 1 every time the loop is executed.
For i = Collection.Count to 1 Step -1
28
Excel VBA Introduction – Part 1

<VBA code to run>


Next i
If there are 5 items in the collection then the variable i will have the value 5, 4, 3, 2 then 1.
Nested For Loop
It is useful to know that loops can be nested. Beginner programmers have a hard to imagine the
concept but eventually you will face them. For example they become useful in UserForms.
Below is how two For – Next loops would look like. When you look at the next loops, think of the
nested one (the y loop) as running five times for each value of the outer loop (the x loop). The loop
prints in the immediate window all combinations of numbers from 1 to 5.
The first five numbers are: 1 – 1, 1 – 2, 1 – 3, 1 – 4 and 1 – 5. In other words for each x from 1 to 5,
print also all values of y from 1 to 5.
Sub TwoNestedFor()
Dim x As Integer, y as Integer
For x = 1 To 5
For y = 1 To 5
' Print all combination of x and y.
Debug.Print x & ", " & y
Next y
Next x
End Sub
With such loop structures you can read/write in a rectangular area of a worksheet provided you know
the width and the height of the area. The next example creates a table of 10 rows by 10 columns and
writes in each cell its address as a relative reference.
Type this small procedure in a module and hit F5 while inside the procedure, then look at the
worksheet.
Sub TwoDimensionsTable()
Dim x As Integer, y As Integer
For x = 1 To 10
For y = 1 To 10
Cells(x, y) = Cells(x, y).Address(RowAbsolute:=False, _
ColumnAbsolute:=False)
Next y
Next x
End Sub
As you explore the use of VBA in Excel using loops and arrays (the topic of arrays is discussed on page
38) you will undoubtedly encounter such constructions.
The For Each – Next flavour
There is a very useful flavour of the For – Next loop called For Each – Next. Here the term Each
refers to individual elements of a collection. With this type of loop you can do something with each
worksheet in a workbook or each cell in a selection.
Because this loop only works with collections it will take a bit of experience before you fully appreciate
it but once you get the gist of it you will use them often. Here an example:

29
Excel VBA Introduction – Part 1

Sub UpperEachCell()
Dim rng As Range
For Each rng In Selection
' Make each cell uppercase.
rng = StrConv(rng, vbUpperCase)
Next rng
End Sub
This sub will change all text entries in the selected cells in uppercase. Use vbLowerCase or
vbProperCase for other use of this Sub.
To loop through each worksheets in a workbook you can use the following:
Sub VisitEachSheet()
Dim wks As Worksheet
For Each wks In Sheets
' Print the name of each worksheet in the Sheets collection.
Debug.Print wks.Name
Next wks
End Sub
You will note that the name of a collection generally is the plural form of a work (Sheets, Names,
Workbooks, etc). You will find more information about collection in the topic: Specifying an Item in a
Collection of page 47 and Looping Through Items in a Collection on page 48.

Passing Parameters
When you create your own procedures you will often (but not always) feed them with a starting value
called a parameter. The parameter is processed by the procedure then it will either do something with
it (if it’s a Sub) or return a value (if it’s a Function)
Parameters are sometimes called arguments and your procedure many require more than one. This
may be easy to understand when you think that most Excel functions require one of more arguments.
The same principle applies here.
The following two sections will show a couple of easy examples:
Supplying a parameter to a function
Let’s start with functions because it is easier to understand why you would do this in the first place.
The following function calculates the area of a rectangle. Knowing the length of both sides of a
rectangle you can easily calculate its area:
Function AreaRect(Length As Integer, Width As Integer) As Integer
AreaRect = Length * Width
End Function
The AreaRect function requires two parameters: Length and Width. Both of them are integers
(whole numbers). There is only one line of code in this function. It simply multiplies the value of the
two parameters and assigns the result to the name of the function. This is how a function knows what
value to return. If A1 contains 3 and B1 the value 5 then entering the formula =AreaRect(A1,B1)
in any cell would return 15.

30
Excel VBA Introduction – Part 1

Your function could also be used in your code instead of in Excel. If you find that you often need to
calculate areas, simply create the function in your workbook and use it anywhere else in the same
workbook.
Examine the next example:
Function PaintCost(Width As Integer, Height As Integer) As Single
' Calculates the cost of the paint.
Dim LitrePaint As Integer, WallSurface As Integer

' Use the AreaRect function.


WallSurface = AreaRect(Width, Height)
Select Case WallSurface
Case 1 To 10: LitrePaint = 1
Case 11 To 20: LitrePaint = 2
Case 21 To 30: LitrePaint = 3
End Select
PaintCost = LitrePaint * 45
End Function
Function AreaRect(Length As Integer, Width As Integer) As Integer
AreaRect = Length * Width
End Function

The PaintCost function needs two parameters: A Width and a Height values. To calculate the
cost of the paint the procedure also needs to know the area of the wall.
Since it doesn’t know how to do this it sends the two measurements to the AreaRect function that
will return that important information. The result of AreaRect is assigned to the variable
WallSurface and the procedure continues.
Depending of the area the procedure calculates the number of litres required. Finally PaintCost
(the function name) is assigned the number of litres times $45.00 (cost of each litre of paint).
To use a custom function in your code (as you would for any standard VBA function) assign the
returned value to a variable this way:
MyVariable = MyFunction(FirstArgument, SecondArgument)
In this case MyVariable will be assigned the value returned by MyFunction.
Supplying a parameter to a Sub
A sub may also require a parameter. Suppose that you often need to add a new worksheet at the end
of the workbook. The name of this worksheet will always be the name of the month specified by a
date parameter.
For example if the date supplied is 5-Nov-2007 then the name of the worksheet should be November.
The following Sub would do just that. Let’s not focus too much on how that procedure works but that a
Sub can accept a parameter.
Sub CreateMonthlySheet(datWhatDate As Date)
Dim strCurrentMonth As String

strCurrentMonth = MonthName(Month(datWhatDate))
' MonthName return the name of the month number.

31
Excel VBA Introduction – Part 1

ActiveWorkbook.Sheets.Add _
After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = strCurrentMonth
End Sub
To call a procedure from within another one type the name of the procedure to run without brackets,
even if it requires an argument. For example:
MyProcedure strStringArgument, intNumericValue
Using the procedure above you could call it from another Sub the following way:
CreateMonthlySheet Date()
This would create a new worksheet with the current month name in the tab. This worksheet would be
the last worksheets in the workbook.

Named Parameters
This section will discuss a common way to specify parameters to methods (or actions) in the Excel’s
object model. This technique is used by many programmers but not by all.
All parameters of a method have a name. If you want, you can give a value to a parameter by
specifying its name prior to the value. Here’s a straightforward example. The MsgBox function can use
up to five parameters. Most programmers only use the first three: A Prompt, the Button configuration
and a Title (in that order).
Suppose that you want to specify only the prompt and the title while accepting the default button
configuration. Here are two ways to write this:
strVal = MsgBox("Do you really need this?", , "Important")
strVal = MsgBox(Title:="Important", _
Prompt:="Do you really need this?")
Although not apparent at first sight there are three important differences between the two
techniques.
 Each parameter is preceded with the name of the parameter separated with a colon and an equal
character.
 Note the two consecutive commas in the first example. They must be used because MsgBox has
three parameters but we are ignoring the second one (accepting its default value). If you skip a
parameter and want to move on to the next one you still need to use the comma.
The second example does not need extra commas.
 Normally the order of the parameters for MsgBox is Prompt, Button and Title. Because the second
example uses named parameters you can specify the parameters in any order you want.
Here is another example. This example uses the Add method of the Sheets collection to add a new
worksheet. Normally the parameters for the Sheets.Add statement are:
Sheets.Add Before, After, Count, Type
If we want to use this code to add 2 worksheets after the Summary worksheets we could specify it in a
couple ways:
Sheets.Add , Sheets("Summary"), 2
Sheets.Add After:=Sheets("Summary"), Count:=2
32
Excel VBA Introduction – Part 1

Sheets.Add Count:=2, After:=Sheets("Summary")


The first example uses one comma because we did not want to specify any value for the Before
parameter. Since we are not using any parameter after Count we do not need to specify trailing
commas. The following would display a Compile Error:
Sheets.Add , Sheets("Summary"), 2,
The last two examples do not require any extra commas because it uses named parameters. Also the
order in which parameters are assigned a value is not important. Some will tell you that you should
always use named parameters and others will tells you that they never bother. Which example to
follow?
It is true that named parameters require more typing on your part but named parameters makes your
code self-documented and this is important. In your opinion which of the two statements (which do
exactly the same thing) is easier to understand?
ActiveWorkbook.SaveAs Filename:= "TheFileName", AddToMru:=True
Or
ActiveWorkbook.SaveAs "FileName", , , , , , , , True
What you need to decide is if the second example is easier to read than the previous one. Especially if
you need to look at other people’s code or if one needs to rework your code months later. The name
of the parameters generally appears after you hit space after the name of the method or property as
shown here.

Just remember that if you are going to use named parameters you must use a colon and an equal sign
to separate the parameter and its value.

The With Statement


If you are using several statements on the same object consider using a WITH statement rather than
fully qualifying the object each time.
It allows you to set up a reference to an object once and then to re-use that reference repeatedly
without having to type it all in again. For example, if we want to address cell A1 on the current
worksheet we would have to refer to it as:
ActiveSheet.Range("A1")
If we now wanted to set the value, the font type and font size for this cell we could use the With –
End With structure as shown below:
With ActiveSheet.Range("A1")
.Value = "Hello World"
.Font.Italic = True
.Font.Size = 16
End With
Note that all properties or methods used in this structure are preceded with a dot. In the next example
the With – End With structure is nested within an If statement.

33
Excel VBA Introduction – Part 1

If Not SheetExist(strMonth) Then


Worksheets.Add after:=Worksheets(Worksheets.Count)
With ActiveSheet
.Name = strMonth
.Range("A1").Value = strMonth
End With
Else
Above the SheetExist statement is a custom function that returns True or False if the worksheet
with the name stored in the variable strMonth exist in the workbook. If the sheet does not exist
then the next line adds it and the two properties Name and Value are updated.
Nested With Statements
You will encounter nested With – End With structures occasionally. Look at the partial section of a Sub
below:
With ActiveSheet
.Name = strMonth
' Add the month name and year in A1
' and enter all the dates in column A.
With .Range("A1")
.Value = strMonth & "-" & Year(Date)
.Font.Size = 16
.Font.Bold = True
.EntireColumn.AutoFit
End With
' Create a date for every day in the month.
For i = 1 To FindHowManyDays(strMonth)
.Cells(i + 1, 1).Value = DateValue(i & "-" & strMonth)
Next i
.Range("A2:A" & i).NumberFormat = "d-mmm"
End With
The nested With – End With structure deals with the object Range("A1"). The outermost With – End
With deals with the properties of the current worksheet. Even in the For – Next loop, the Cell object
belongs to the ActiveSheet because it is outside the nested With – End With but still inside the
outermost one.

Debugging Your Code


There are many types of errors that can creep into your code. Some errors are pretty obvious like
syntax and compile errors. The latter errors will prevent your code from running and the guilty line
will be highlighted in yellow.
There are also runtime errors that occur when the code executes. This would happen if you, for
example, assign a string value to a variable declared as numeric. But there is a worst type of error: a
logic error.

34
Excel VBA Introduction – Part 1

Logical errors are the hardest one to locate because there is nothing wrong with the code. The error is
in the reasoning or the logic followed by the procedure. The VBE offers many tools to trap or zero-in
on a logical error and fixing it.
The Debug.Print statement
As we have seen in this course you can use the Debug.Print statement to dump a value in the
Immediate window. This allows you to save a copy of the value a variable has at some point. You can
print any VBA expression in the Immediate window. For example the statement:
Debug.Print "After " & i & " iterations the total is " & intTotal & "."
May print the following result: ‘After 5 iterations the total is 28.’
You will find that the Debug.Print statement is very useful in loops where a variable will take on
multiple values.
Using Breakpoints to stop the code
A breakpoint is a line where the code will stop
executing and enter Break mode. The line having a
breakpoint is not executed. Rather it waits for you
to resume the code in a couple of ways.
To set a breakpoint in your code move to the line where you wish to enter in Break mode and hit F9.
The whole line is highlighted in burgundy and a dot (a margin indicator) appears in the left margin.
Note that you cannot set a breakpoint on a line using the Dim statement or if it is blank.
When the procedure runs it will stop dead on the
line having a breakpoint. You will be in Break mode.
The VBE title bar will display [break] next to the
workbook name.
In Break mode you step into your procedure one line at a time by pressing F8. The yellow bar will move
one line down and wait. If you want you can hit F5 to resume the procedure or click on Reset to exit
Break mode. For more information on this topic see page 20
In Break mode you can see the value of a variable by moving your mouse over a variable or an
expression returning a value. This is very handy when you
are examining how a variable is doing in a loop.
On the right you can see that the current value for the
variable Cummulative is 12.
The Current Line of Execution
You noted the yellow arrow margin indicator in the left
margin. VBE calls this arrow the current line of execution. It
is pretty easy to understand that it points to the line that
will be executed next.
What is interesting about this arrow is that you can drag it
up or down in the margin so that you specify which line will run next. This allows you to re-run a
couple of lines or to skip some.

35
Excel VBA Introduction – Part 1

Some bugs are pretty nasty and need a thorough examination of how the code run before you discover
them.
About the Immediate Window
As you have seen in depth in the Excel objects section on page 17 while you are in Break mode you can
interact with Excel and interrogate variables and properties by typing expressions in the Immediate
window. When your procedures are in Break mode you can type an expression in the Immediate
window using any variable in your code to test for a value.
For example if your code has a variable called strSuburb and it is assigned a value you can retrieve its
value by typing (don't forget that you must be in Break mode to do this):
?strSuburb
You can type more elaborate expression too. For example (this one uses two variables):
?Mid(strSuburb,intPosition,2)

Finally, in a procedure using a selection assigned to the variable rng and two variables intRows and
intCols equal some value, you can type statements like:
?rng.Cells(intRows,intCols)

This expression prints the value of the cell intRows down and intCols across from the top left
corner of the selection assigned to rng. If, for example, intRows = 3 and intCols = 2 then the
above expression returns the value of the cell in the third row and second column or the selection.
The lesson here is: Do not forget the immediate window! It is not for nothing that it used to be called
the Debug window. It is an invaluable element of the VBE.
Adding a Watch
Last but not least is the Watches window. This window allows you to keep an eye on the evolution of
as many variables as you want. You do not need to be in Break mode to add variables. In this case they
will contain no value. The Watches window will display <Out of context> in the Value column.
Add a variable to the Watches window by right-clicking a variable name and choosing Add Watch. The
Add Watch dialog box appears to confirm that you want to ... watch that variable. Click OK to confirm
and the Watches window appears. Below are two variables in the Watches window while in Break
mode. You can see the current value of the two variables.

To watch another variable, double-click the variable name and drag it inside the Watches window. To
remove a variable from that window select it and hit Delete.
What is truly interesting is that you can change the value of a variable in the Watches window to test
what happens if it has this value. Simply click the current value and type a new value. This way you can
examine the behaviour of a procedure if a variable had a particular value.

36
Excel VBA Introduction – Part 1

To remove an entry from the Watch window click the spectacle icon at the far left and hit Del.

Error Handling
The expression Error Handling refers to the programming technique dealing with an error in your code.
From the outset you need to understand that we are not talking about bugs in your code but a
situation that may arise that prevents your code from executing. This type or errors are called run time
errors.
An easy run time error is if the user attempts to rename a worksheet using a name that already exists
in the workbook or code that divides the value of two cells and the second one contains a zero.
The On Error Statement
For a procedure to trap any error you must use the On Error Goto <label> statement at the top of your
procedure. This statement tells the procedure what to do if an unexpected error occurs. These
instructions are specified in the same procedure near the end of the procedure and they are located
using labels.
Here's a typical easy example:
Sub CreateNewSheet(strName As String)
On Error GoTo CreateNewSheetError

ActiveWorkbook.Sheets.Add _
After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = strName

CreateNewSheetExit:
Exit Sub

CreateNewSheetError:
If Err.Number = 1004 Then
MsgBox "Sorry the name already exist."
Else
MsgBox Err.Description
End If
End Sub

The Sub CreateNewSheet creates a new worksheet at the end of the workbook sheets and gives it
the name specified as the argument (strName). The line On Error Goto specifies that, if a run
time error occurs for any reasons, the code after the label CreateNewSheetError is to be
executed.
A label is simply a single word (no space in other words) followed with a colon. Under this label you
will find an If statement that checks the error number and act accordingly. In this example the error
number 1004 is the "Cannot rename a sheet to the same name of another sheet.". After the message
is acknowledge by the user the code resumes after the End If.
What if there is no error in the procedure? If Excel does not return an error executing the
ActiveSheet.Name statement then it goes on with the next statement. In this case it is Exit
Sub (Labels are not VBA statements per see).
What do you think would happen if the following lines did not exist in the sub?
CreateNewSheetExit:

37
Excel VBA Introduction – Part 1

Exit Sub
When a run time error occurs you will see the error number at the top of the dialog box displaying the
error. Note the number and use it in the If Then Else structure to react accordingly.
On the right you see the error message. If you want to know the
error number after you have introduced an error handler (in
which case the dialog box will not appear) you can use the
following statements in your If statement to print the error
number and text respectively:
Debug.Print Err.Number
Debug.Print Err.Description
If you are in Break mode you can also type in the Immediate window:
?Err.Number
?Err.Description
The Resume Next Statement
The Resume Next statement is useful when the error is not critical (even possibly expected) and this
will not stop the execution of your code. Here the term Next refers to the line after the one causing
the error. Look at the following example:
Sub DivideCells()
On Error GoTo DivideCellsError
Dim rng As Range

For Each rng In Selection


rng.Offset(0, 2) = rng / rng.Offset(0, 1)
Next rng

DivideCellsExit:
Exit Sub

DivideCellsError:
rng.Offset(0, 2) = 0
Resume Next
End Sub

This DivideCells procedure divides each cell in the selection by the cell immediately to its right
and writes the answer in the third column (two cells to the right of the selection).
You will note that the second column contains an occasional zero. Because it is impossible to divide by
0, the procedure would stop when it encounters the first 0 if there was no error handler. But because
there is an error handler the code after the DivideCellsError label is executed.
The DivideCellsError section simply writes a 0 in the third column then resume execution on
the next line where the error occurred. In this case it is the line with the statement Next rng. In
other words in the cell to the right of rng = 0 then write a 0 in the third column and do as if there was
never an error, it was taken care of.
Once the procedure is done with each cell in the selection the procedure ends and runs the line in the
DivideCellsExit label which is Exit Sub.

38
Excel VBA Introduction – Part 1

Understanding Arrays
As you learn more about programming Excel you will eventually encounter the term array. For
example, if you have used the VLookup function you may remember that the name for the second
argument is table_array. In that context, array simply means the database that is being searched and
which will return the value sough.
You may also have heard of array formulas. This special type of formulas uses ranges of cells as
arguments where you would normally use one cell only. Some array formula can also return multiple
values (an array).
Although arrays are not exactly the same in VBA, we can learn something from the term used in
functions or with array formulas: An array is an object that contains multiple values. In VBA you
declare a variable using a special syntax where you specify as many values as necessary for that array
variable.
You assign and access individual element of an array by using its unique index value.
About arrays and index
To be able to differentiate various elements of an array they are assigned an integer number starting
at 0. The first element has the index 0, the second one is indexed 1, and so on. It takes a little while to
get used to having the first element labelled 0.
In a new module type this small procedure:
Sub MyFirstArray()
Dim aryFruits As Variant
aryFruits = Array("Apple", "Kiwi", "Banana")
End Sub
While the insertion point is inside the procedure hit F8 three times. You are now in break mode and
the last line is highlighted. This means that the variable aryFruits was initialized and assigned the
name of three fruits.
In the Immediate window type the following expression and hit Enter:
?aryFruits(0)
The string Apple is returned. The index 0 contains the first of a three element array. Try with the index
1 and 2. Now try using the index 3 and look at the message.
Looping through an array
Modify the above procedure as follow:
Sub MyFirstArray()
Dim aryFruits As Variant, varFruit As Variant
aryFruits = Array("Apple", "Kiwi", "Banana")
For Each varFruit In aryFruits
Debug.Print varFruit
Next
End Sub
While the insertion point is inside the procedure hit F5 to run it. The procedure will print in the
Immediate window all the elements of the array. Try adding another couple of fruits in the Array

39
Excel VBA Introduction – Part 1

function. You will learn more about this type of loop in the section Looping Through Items in a
Collection on page 48.
Declaring Arrays
There are a couple of ways you can declare (or Dim) arrays in your code. In the previous example we
explicitly assigned three pieces of fruits to the array variable. Not very flexible you might say.
If you know how many elements the array will contain you can use this value in its declaration. Look at
the following example:
Sub AnotherArray()
Dim aryNames(3) As Variant
Dim intCounter As Integer
For intCounter = 0 To 3
aryNames(intCounter) = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Next intCounter
For intCounter = 0 To 3
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = aryNames(intCounter)
Next intCounter
End Sub
In this example we know that the array will contain four values (remember the first element is 0). This
is why they are called fixed-size arrays (or static arrays). It then reads the value of four cells starting
with the active cell and finally creates four new worksheets using the names in the array.
To try this procedure type four names in four cells going down. Move the active cell to the first one.
While the insertion point is in the procedure hit F5 to run it in one go of press F8 to run it in break
mode. Remember to delete the four worksheets if you want to run it more than once.
The next example shows how to use an array to do something with multiple worksheets. In this case
the Sub deletes them but we could move, copy, hide or print the sheets contained in the array.
Sub DeleteTheSheets()
' Delete the first four sheets in the active workbook.
Dim arySheets(3) As Variant, intCounter As Integer
For intCounter = 0 To 3
arySheets(intCounter) = Sheets(intCounter + 1).Name
Next intCounter
Application.DisplayAlerts = False
ActiveWorkbook.Sheets(arySheets).Delete
Application.DisplayAlerts = True
End Sub
Inside the For loop note the expression Sheets(intCounter + 1). The reason for this is that
arrays are zero based but not the Sheets collection (which is not an array). So arySheets(0) is assigned
Sheets(1), arySheets(1) is assigned Sheets(2), and so on.
Here are other examples of statements you could have used in this procedure:
ActiveWorkbook.Sheets(arySheets).Visible = False

40
Excel VBA Introduction – Part 1

ActiveWorkbook.Sheets(arySheets).Move Before:=Sheets(1)
ActiveWorkbook.Sheets(arySheets).Printout Preview:=True
ActiveWorkbook.Sheets(arySheets).FillAcrossSheets _
Sheets(1).Range("A1")
The first example makes all sheets in the array invisible. The next example moves all sheets at the
beginning of the workbook. The third one displays all the sheets in Print Preview. In the last example
the value of cell A1 in the first sheet is copied in cell A1 of all the worksheets in the array. Powerful?
Indeed.
What is the size of an array?
If you want to know how many elements an array contains use the UBound function (Upper Bound).
Note that UBound will return the last index number and recall that arrays are zero based. You will
need to adjust the value returned by UBound to reflect the number of elements in the array.
Suppose an array contains five cities then the last element will have the index 4. To store the number
of elements is the variable intQuantity use the following:
intQuantity = UBound(aryCities)+1
Use the LBound function (Lower Bound) to know the index of the first element of an array. For
example the following For loop will consider each value of an array of unknown size.
For x = LBound(MyArray) To UBound(MyArray)
<VBA code to run>
Next x

Dumping an array in a worksheet


There are times when you need to dump all values of an array in a worksheet. The next example prints
the four values in the aryDepts array in the first sheet and sorts the resulting list.
Sub PrintSortArray()
Dim aryDepts(3) As Variant
aryDepts(0) = "Marketing"
aryDepts(1) = "Accounting"
aryDepts(2) = "Sales"
aryDepts(3) = "Finance"
Sheets(1).Range("A1:A4") = _
Application.WorksheetFunction.Transpose(aryDepts)
Range("A1:A4").Sort Key1:=Range("A1")
End Sub
Many developers simplify the line above by omitting WorksheetFunction after Application.
The following would work perfectly:
Sheets(1).Range("A1:A4") = Application.Transpose(aryDepts)
Most readers would have noted that all the array examples to this point deal with a predetermined
number of elements (as in the example above.) The next section will show you how to work with
arrays when you do not know its size beforehand.

41
Excel VBA Introduction – Part 1

About dynamic arrays


Dynamic arrays have a major advantage over fixed-size arrays: You can specify the size at runtime and
it may change during the execution of the procedure. You declare a dynamic array the same way fixed-
size array except that you do not specify the size. For example:
Dim arySheets() As String
Then when you know how many elements the array should contain use the Redim statement like this:
ReDim arySheets(intValue)
To illustrate, the following example will print in column A of the first sheet the names of all the
worksheet in a workbook. Note that the number of sheets can change at any time.
Sub ListSheets()
Dim arySheets() As String, intCount As
1 2 3 4
Integer 2 4 6 8
ReDim arySheets(Sheets.Count) 3 6 9 12
For intCount = 1 To Sheets.Count 4 8 12 16
arySheets(intCount - 1) = 5 10 15 20
Sheets(intCount).Name 6 12 18 24

Next intCount

Sheets(1).Range("A1:A" & intCount) = _


Application.Transpose(arySheets)
End Sub

About the Redim statement


In the previous example we used the ReDim statement to specify at runtime the size of a dynamic
array. You should be aware that this statement will create a copy of your array without the elements
of the original array. This means that the new array will be empty. One way to prevent this from
happening is to use the Preserve keyword after ReDim. For example:
ReDim Preserve aryMyArray(intNewValue)
This will increase the size of the array to the value of the variable intNewValue. Now Preserve is really
useful but it needs to create a new array with all the elements of the previous array. This means a lot
of overhead or slow execution.
Some programmers will increase an array by a value larger than needed at the time to avoid using
ReDim every time a loop is executed.
Two dimensional arrays
Elements of an array can also contain pairs of data. The following example declares a two dimensional
array able to contain 24 pairs (a 4 x 6 array):
Dim aryData(0 To 3, 0 To 5) As Integer
To assign pairs of data to each element of an array you need to use two For-Next loops embedded in
one another. Look at the following example:
Sub TwoDimArray()
Dim intCol As Integer, intRow As Integer
Dim aryData(0 To 3, 0 To 5) As Integer
42
Excel VBA Introduction – Part 1

For intCol = 0 To 3
For intRow = 0 To 5
aryData(intCol, intRow) = (intRow + 1) * (intCol + 1)
Next intRow
Next intCol
Range("A1:D6") = Application.Transpose(aryData)
End Sub
This procedure will create an array of 4 columns by 6 rows populated by the multiplication of the index
values of each element of the array.
Because arrays are zero based we need to add 1 in the multiplication. For example the element (1, 4)
contains the value 10 (2 x 5) and the element (3, 5) (4 x 6) contains the value 24.
The whole array is then dumped in a range of 24 cells as shown on the right.
In conclusion
If you need to program Excel Userforms containing dropdown lists or list box controls array will
become an essential tool to populate these controls. You can go a long way in Excel VBA without using
arrays but keep in mind that you will eventually need to work with them.

43
Excel VBA Introduction – Part 2

Part 2 - More about Excel’s Object Model


If you want to progress in your understanding of programming Excel you need to get familiar with its
object model. Programming Excel’s object model is what makes the difference between running code
in Access or Word and programming in Excel.
As we’ve seen in this course, many applications use VBA as the programming language but each has
their own object model.
Another reason why you may want to learn about an applications hierarchy of objects is that it is
reasonably easy to take control of an application from within another. For instance you may want to
use Word’s mail merge feature with a list of addresses in Excel or use Outlook to send an email to
recipients whose addresses are in a worksheet. Finally you might decide to use Access powerful report
generator with a list currently in Excel.
Whenever you do something in a workbook you are issuing commands through the Excel object
model. Here’s an easy example. When you close a workbook and save the changes you are actually
using the ActiveWorkbook.Close True statement. Here the True parameter specifies to save
the modifications to the workbook.
In the example above, the Close method acts upon the ActiveWorkbook object. Here is another
example. Type the following in the Immediate Window:
ActiveWorkbook.BuiltinDocumentProperties("Title")="Budget 2008"
Then choose File > Properties5 and note the value for the Title built-in property. Because you use the
assignment character (=) you are thus assigning a value to the Title property of the active workbook.
Now if you type instead:
?ActiveWorkbook.BuiltinDocumentProperties("Author")
You are inquiring about the value of the Author property. Can you appreciate how you can change
these properties through the user interface or by setting two properties of the ActiveWorkbook
object with VBA?
What is ‘magical’ with programming the object model of an application is that you have access to all
the objects and actions so that you can do everything in code that can be done through the user
interface and much more. All this because Excel’s functionality in contained in its object model.
The Excel object model contains a large number of objects — for example, Workbooks, Worksheets,
Range, Charts, PivotTables, and Comments. These objects are standalone entities that offer various
pieces of functionality for your analysis but keep in mind that they all can be controlled from your
code.
About The Term Object
It is impossible to learn about programming without facing the term object sooner or later. Defining
this term can be difficult depending how far we want to learn about them. Here’s one I found and I
think is just simple enough:

5
In Excel 2007 click the Office button then Prepare then Properties.
44
Excel VBA Introduction – Part 2

An object is a programming structure encapsulating both data and


functionality that is defined and allocated as a single unit.
In other words an object is a self-contained entity that I can interrogate or use to do an action. The
object model organizes all Excel’s functionality in a complex hierarchy of objects available to you.
Want to see the Excel’s object model? In VBE choose Help > Microsoft Visual Basic Help. In the Table
of Contents choose Microsoft Excel Visual Basic Reference to open that thread. The first item in the
list is Microsoft Excel Object Model. Don’t worry you do not have to learn all this by heart. You simply
need to know how to use it to find useful information.
At the bottom of the page you will see a legend that identifies the yellow boxes as Object and
Collection and the cyan boxes as Object only. This page works like any Web page so all boxes are
hyperlinks to another page giving more information about that object (or collection).
About The Term Collection
You will notice that the names of the Collections are in the plural form. This makes sense because a
collection is a bunch of related objects right? So the Stamps collection contains all the Stamp objects
in that collection but the collection itself is also an object — it is a collection. You can do something
with the collection like adding or removing a stamp. You can also count how many stamps in the
collection.
Click the Names collection to see the Help window on the Names collection. In the diagram at the top
you see that the Names collection contains Name objects. Below the diagram you will find the
following definition:
A collection of all the Name objects in the application or workbook.
Each Name object represents a defined name for a range of cells.
If you click the Properties link at the top you will find a reference to a Count property. This property
returns the number of range names in the Names collection (in the active workbook).
In the Method link at the top you will find a reference to the Add method. Click on it to follow the link.
In the Example section below click As it applies to the Names object to see an example of creating the
name tempRange to the range A1:D3.
Click Back in the Help window to return to the Names Collection window. Did you note that there was
no Delete method? This is because you don’t want to remove the Names collection. On the other hand
you may want to delete one item in that collection — one Name object. In the diagram click the Name
object blue box.
The Name Object page describes what you can do with a single name. Click the Method link to see the
list. Click Delete in the list. Some help about the Delete Method appears but nothing specific to
deleting a Name object in a workbook. Nevertheless you can see an example or two that you can learn
from.
To delete a name from the active workbook use the following example:
ActiveWorkbook.Names("RangeName").Delete
In essence, we are deleting the name RangeName inside the active workbook from the Names
collection.

45
Excel VBA Introduction – Part 2

About Properties and Methods


All objects in an object model have properties and methods. Some have many some have very few,
depending on the complexity of the object. A property is a scalar6 attribute that defines various
attributes (characteristics) of an object.
The properties available vary greatly from one object to another and their value can be text or
numeric. Here is an easy example. Create a new workbook then in the Immediate Window type:
Worksheets("Sheet2").Columns(3).Hidden = True
Here are two more collections; the Worksheets and the Columns collections. Here we are establishing
a reference to the worksheet object having the Name property equal to Sheet2. Note the similitude
with the previous example where we established a reference to the name RangeName using
Names("RangeName") qualifier.
A worksheet has a Columns collection and we want to establish a reference to the third column
(Column C). All Column objects have a Hidden property that can be set to True (-1) or False (0).
You will have figured out that the line of code you typed made the column C invisible. By the way you
could also have used Columns("C:C") or simply Columns("C") but it is extremely important to
remember that you can also use its index number.
You can also inquire about the value of a property. To do this use the property name without the
assignment character (=). The following line returns the name of the last sheet in the active workbook.
Worksheets(Worksheets.Count).Name
If you don’t establish a reference to a particular workbook the active workbook is assumed. For
instance Worksbooks("MyBook") will give you access to all objects in that workbook.
Small assignment: If the active workbook is First.wks and you want to establish a reference to the last
worksheet in Second.wks what would the line of code be?
Methods are ways of executing actions on a particular object. We could also say that a method gives
you access to a procedure associated to an object that will act on that object. A method often requires
one or more arguments.
One homely example is the Copy method which is one of the methods used with the Range object. The
following example copies the content of cells A1 to the clipboard. The user decides where to paste it
manually.
Worksheets("Sheet1").Range("A1").Copy
The VBA help file also mentions that you can use the Destination parameter to specify where you want
the copy to be. It can be anywhere in the same sheet, another worksheet even in another workbook.
The next example copies the value of cell A1 in Sheet1 of the active workbook to cell A1 in sheet WS in
the workbook WB.xls.
Worksheets("Sheet1").Range("A1").Copy _
Destination:= Workbooks("WB").Worksheets("WS").Range("A1")

6
A quantity that is defined by its magnitude only (ie position, name).
46
Excel VBA Introduction – Part 2

Specifying an Item in a Collection


Up till now we have seen two ways to establish a reference to one item in a collection — by its name
or by its index. But there is a third one. All of them are important and are useful when you need them.
The first one, using the item’s name follows this syntax:
Collection("Name")
The name is always a text string. Here are a couple of examples:
Worksheets("Sheet1")
Charts("Chart1").SeriesCollection("Region1")
PivotFields("Revenu")
Next is by its index value. Simply specify the value in parentheses. The first item in the collection has
the value of 1. For example:
Worksheets(1)
Charts(1)
Although you may not see a use for using the index number right now it is a very efficient way to loop
through each item in a collection.
The last way is to use a variable. Assign a value to a variable then use it later in your code.
Dim strWS As String
strWB = "Sheet3"
WorkSheets(strWS).Activate
Everywhere in this procedure where strWS is used it will be equivalent to specifying Sheet3.
Consider the following CreateNewSheet procedure. It requires one argument; the name of the
worksheet to be created.
Sub CreateNewSheet(strSheetName As String)
On Error GoTo CreateNewSheetErr

' Create sheet after the last sheet in the workbook.


Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = strSheetName

CreateNewSheetExit:
Exit Sub

CreateNewSheetErr:
If Err.Number = 1004 Then
MsgBox "Already a worksheet with that name!"
End If
Resume CreateNewSheetExit
End Sub

47
Excel VBA Introduction – Part 2

Looping Through Items in a Collection


Let’s learn how to loop through each item in a collection. The For Each — Next construct makes
it very easy to do this. Remember our Stamps collection? To read the year each stamp in our stamps
collection was issued we could write:
Sub PrintYearOfStamps()
Dim st As Stamp ' Stamp singular
For Each st In Stamps ' Stamps plural
Debug.Print st.Year ' Year is a property of a stamp
Next st ' Next stamp
End Sub
Let’s see a real example: Looping through all the Worksheet objects in the Worksheets collection.
Create a new workbook containing five of six worksheets. Examine the following example:
Sub ListSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Debug.Print ws.Name
Next ws
End Sub
You just need to create one variable of the same type as the objects in the collection and use it in the
For Each — Next loop. While you are between the For Each and the Next statements you
have access to all properties and methods of the current object.
Once the Sub have looped through all objects in the collection it ends and execution resumes after
the Next statement.
You can also examine each cell in a selection. If you look up Selection in Help you will find that if there
are cells selected in the worksheet (as opposed to a chart object) then Selection returns a Range
object. The Range object represents the collection of all cells in the selection.
The next example examines each cell in the selection and increments the variable intCounter each
time the current cell contains a formula. It also prints in the Immediate Window the address of these
cells and to total number of formulae.
Sub FindFormulas()
Dim rng As Range
Dim intCounter As Integer

For Each rng In Selection


If rng.HasFormula = True Then
intCounter = intCounter + 1
Debug.Print rng.Address
End If
Next rng

Debug.Print intCounter & " formula found."


End Sub

48
Excel VBA Introduction – Part 2

The HasFormula property returns True if a cell contains a formula. When the procedure finds a
formula it increments a variable and print the cell’s address in the Immediate Window.

Entering Formulas in a Range


One of the reasons you are learning VBA is to automate the creation of flexible formulas. Depending
on the situation the formula may be in a different cell each time. The range referenced by the formula
may also change.
Excel uses two notations to refer to cells in formulas. The first one is called the A1 notation (also
known as the A1 reference style). In this notation all cells are referred to by their column – number
coordinates. Examples are B5 and C4:C8.
Although this is the notation of choice when working in the Excel interface it is not always practical
when writing macros. For example, suppose you need to average the five cells to the left of the active
cell. Of course you do not know the address of the active cell because every time you run the macro
it’s at a different location.
Instead of trying workout the range of those five cells using the A1 reference style use the R1C1
reference style where you can specify that you want to refer to ‘the five cells to the left of the active
cell’ easily.
You can add a formula in a cell using two properties: Formula and FormulaR1C1. The Formula property
allows you to write your formula using the same notation you see in the Formula Bar. For example
type 6, 8 and 5 in A1:C1 then move to D1. Type the following:
ActiveCell.Formula="=AVERAGE(A1:C1)"
Will return 6.3
Using the FormulaR1C1 property you would use:
ActiveCell.FormulaR1C1="=AVERAGE(R[-3]:C[-1])"
Both would return the same answer. The first example uses the A1 notation and the second one the
R1C1 notation. This is the notation used by the macro recorder. Before we explain, note that in both
cases the formula is a string and start with the equal sign.
Both examples above create a formula in the active cell. To add a formula to a selection of cells use
Selection instead of ActiveCell. These will be an example of this technique in the following
section.
The R1C1 Notation
The R1C1 notation is very flexible and confusing at first. It easily allows you to create relative, mixed
and absolute references. The R and the C, as you might have guessed, stand for Row and Column. In
this reference style use an R and a number as well as a C with a number to create a reference.
If you do not specify a number next to the R then you are referring to the same row as the active cell.
Similarly omitting the number along with the C will refer to the same column as the formula.

49
Excel VBA Introduction – Part 2

By nature the R1C1 notation is an absolute reference. So R1C1 always refer to cell $A$1 and R5C8 will
refer to $H$5. Fortunately you will rarely use the R1C1 notation this way7.
In general you will need to refer to a cell or a range using a relative reference.
With the R1C1 reference style you use the square bracket to specify the
number of rows and columns to offset from the active cell. For example
R[1]C[1] always mean one cell below and one column to the right from the
active cell wherever it is.
The table to the right shows you how to refer to the eight possible cells
around the active cell. As you can see, omitting the row or column number
will refer to the same row of column as the active cell. You could also use R[0]C[1] or R[1]C[0] to
achieve the same thing.
Type the following two rows of four numbers each anywhere in a worksheet then select the two cells
past the last column as shown here.

In the Immediate Window enter the following line:


Selection.FormulaR1C1="=SUM(RC[-4]:RC[-1])"
Look in the Formula Bar. The two cells contain the SUM function referring to the four cells to the left of
the formula.
Type the following table (on the right) anywhere is a worksheet except for
the value of the payment. The function for the calculation of the payment
is:
=PMT(Rate/12,Months,-Amount)
Move the active cell next to the Payment label and in the Immediate Window enter the expression
that will create the result as shown on the right. The following is a hint:
ActiveCell.FormulaR1C1="=PMT(type references here)"
Once you’re done you should see the same amount shown here.
One of the really powerful uses of the R1C1 reference style is that you can use variables instead of a
number. Since you may not always know how far to go from the active cell, you could store in a
variable the width of a table of the height then use that information in the formula.
To use a variable with the R1C1 reference style you need to concatenate the variable(s) in the
expression. Suppose that the variable intTableWidth contained the width of the table, you could
use the following expression in your VBA code:
"=SUM(RC[-" & intTableWidth & "]:RC[-1])"

7
If you need to always refer to the same cell you might consider using a named cell instead.
50
Excel VBA Introduction – Part 2

If you have a hard time figuring out how to convert a formula from the A1 style to the R1C1 reference
style you can use the ConvertFormula method of the Application object. It allows you to convert
any references from one notation to another. Here’s an example. If the active cell is in C6 the following
expression:
Application.ConvertFormula("=SUM(C1:C5)",xlA1,xlR1C1)
Will return:
=SUM(R[-5]C:R[-1]C)
Another way you can do this is to interrogate the Formula and the FormulaR1C1 property. As you
know all cells have a Value, a Formula and a FormulaR1C1 property. The Value property returns the
value in the cell (maybe the result of a formula). The Formula property will return the formula as
shown in the Formula Bar and the FormulaR1C1 which will return the formula using the R1C1
reference style.
Try ?ActiveCell.Formula or ?ActiveCell.FormulaR1C1 in the Immediate Window to
display the formula in the active cell in the two reference styles.

Exploring the Range object


There are so many properties for the Range object that it would require many more pages to cover
them all. In this section we will examine some of the most common ones.
The use of the Range object is so common in Excel VBA, you should make sure you are familiar with its
basic properties and methods. In VBE choose Help > Microsoft Visual Basic Help. In the Table of
Contents choose Microsoft Excel Visual Basic Reference to open that thread. Click Collections then
click R then click Range Collection.
Browse the information in this help page and explore the Properties and Methods links at the top. You
can refer to a range using multiple way. Here’s couple of example:
Range("C3") .......................................... Cell C3.
Range("C1:C3") ..................................... Cells C1 to C3 .
Range("B1:B3,J15:J18") ........................ Two ranges.
Range("Loan") ....................................... Range name Loan.
Range(Cells(1,1),Cells(3,3)) ................... Cells A1 to C3
Each technique above gives you access to all the various properties of ranges. You may think of the last
one as being tedious but remember that each method has its advantages and works best is certain
circumstances.
Referencing a Range
As seen previously it is very useful to create a reference to a range before using it in your code. This is
especially true if the range may change size while the procedure is running. So at the top of your
procedure use a line like the following:
Dim rng As Range
Dim rng2 As Range
Of course the name of the variable can be anything you like. Later in your code you then need to
assign a range (a cell or a selection) to the variable using the Set statement. For example:

51
Excel VBA Introduction – Part 2

Set rng = Range("A1:C5")


Set rng = ActiveCell.CurrentRegion
Set rng2 = rng.Resize(rng.Rows.Count+1,rng.Columns.Count+1)
The first one is pretty straightforward. The next one assigns to the object variable the whole region
containing the active cell. If you know for sure that the region will always start in cell A1 you can use:
Set rng = Range("A1").CurrentRegion
The third example above uses the Resize property to return a new range based on the height and
width of rng. The new range is one row and one column larger than rng. Resize is discussed shortly.
The CurrentRegion property
A popular way to reference a range is to use CurrentRegion property. When you press Ctrl+* in
Excel it select the whole region surrounding the active cell. For example create a small table in a
worksheet and make sure the active cell is anywhere in that list. The following code assigns the whole
region containing the active cell to the variable rngTable.
Sub SelectMyTable()
Dim rngTable As Range
Set rngTable = ActiveCell.CurrentRegion
End Sub
Note that when recording a macro Ctrl+A and Ctrl+* is very different. The statement
CurrentRegion is recorded only when Ctrl+* is pressed.
The Columns and Rows Properties
The Columns and Rows objects are collections containing the columns and rows in the worksheet,
the selection or the Range object. Both properties have a Count property that will return the number
of columns and rows in a range. So:
ActiveSheet.Columns.Count Return 256 in Excel 2003
Selection.Columns.Count How many columns selection.
Rng.Columns.Count How many columns in rng
You can use the same technique for the Rows collection.
If you need to know the size of the range you can use two simple properties — Columns and Rows.
Examine the modified procedure:
Sub SelectMyTable()
Dim rngTable As Range
Dim intWidth As Integer, intHeight As Integer
Set rngTable = ActiveCell.CurrentRegion
intWidth = rngTable.Columns.Count
intHeight = rngTable.Rows.Count
Debug.Print "The region is ” & intWidth & _
" columns wide by " & intHeight & " rows high."
End Sub
Running this procedure in a list 12 columns wide by 68 rows high to display:
The region is 12 columns wide by 68 rows high.

52
Excel VBA Introduction – Part 2

The Resize Property


This property resizes the specified range and returns a range with the new size. Depending on the
situation, the data may have changed since the last time you ran your code so the range you are using
in your code needs to adapt. You may need to resize the width or the height only.
Since Resize returns a Range you can assign that new range to a Range variable (if you want to refer to
that new range later). The Resize property asks for a new row height and column width (measured
in cells). Here is an example:
Set rngSummary = rngSummary.Resize(10, 1)
The rngSummary range variable is redefined and will now have 10 rows high by 1 column wide.
Here’s a couple of other example of how to use the Resize property.
rng.Resize(100,80).Select Resize and select.
rng.Resize(rng.Rows.Count+1) One row higher.
rng.Resize(,rng.Columns.Count+1) One column wider.
rng.Resize(ColumnSize:=rng.Columns.Count+1) Same as above.
If you don’t need to store the resized range in a variable you can use the following:
Selection.Resize(new row size, new column size).Select
This will resize the current selection but not save the new selection in a Range variable and this is
perfectly acceptable.
Suppose rng is assigned C3:F6 and the following line of code is executed:
Set rng2 = rng.Resize(rng.Rows.Count+2, rng.Columns.Count-1)
Write what the following statement would return in the Immediate Window:
?rng2.Address
Small assignment: Suppose your code imports data and you need to add a formula in the first column
to the right of that table. The problem is that you never know how many rows your list will have. You
only need to find the cell that will have the formula and resize that range to the height of the list.
Make a selection of a few rows and columns then type one number in each cell. Make sure the active
cell is anywhere in the region. Type the following code and run it one at a time (Using F8):
Sub RightSummary()
Dim rng As Range, rngRight As Range
Dim intCols As Integer, intRows As Integer
Set rng = ActiveCell.CurrentRegion
intCols = rng.Columns.Count
intRows = rng.Rows.Count
' Find the first cell past the last column.
Set rngRight = rng.Cells(1, intCols + 1)
' Resize that cell to the height of the table.
Set rngRight = rngRight.Resize(intRows, 1)
rngRight.Select
End Sub

53
Excel VBA Introduction – Part 2

Once we have found the region and the number of rows and columns, locate the first cell in the next
column at the right of the table. Note the use the Cells property to establish the reference to that cell
in the first row and intCols + 1 columns across. The Cells property is discussed shortly.
Once rngRight point to that cell, it is redefined (resized) to include the number of rows in the table.
The line rngRight.Select selects the cells just to show that the range has been resized. In you
applications you would immediately set the FormulaR1C1 property with whatever calculation you
want to do with the list. For example:
rngRight.FormulaR1C1 = "=SUM(RC[-" & intCols & "]:RC[-1])"
This would sum all the numbers to the left.
The Offset property
The Offset property returns a range that is shifted from the specified range. It has the same size as the
referenced range. This can be useful if you want to create a new range with exactly the same size of
another one, especially if you cannot predict the size of the original range.
The Offset property can use a horizontal (column) and a vertical (row) measurement. You can use both
or only one. Suppose rng is assigned to the range A1:A10 look at the following examples:
rng.Offset(0, 5).Select
rng.Offset(, 5).Select
Set rng2 = rng.Offset(intRows, intCols)
Selection.Offset(0, 10).Select
The first two examples do the same thing; select the cells 5 column to the right of rng with the same
size as rng. As you can see if the offset is 0 you can skip this parameter although it is self documenting
to specify the 0.
The third example assigns to the variable rng2 a new range shifted from rng the specified number of
rows and columns stored in the variables.
The last example selects the cells 10 columns across. The new selection has the same size as the
original one.
Small assignment: Suppose rng is assigned the range A1:D10, can you figure out what the following
example will do.
Set rng2 = rng.Offset(1, 0).Resize(rng.Rows.Count-1)

Answer:
The Cells property
Another very useful property of the Range object is Cells. It returns a one cell range from the specified
(larger) range. You refer to a specific cell by specifying its row and its column number. For example:
rng.Cells(row number, column number)
For example:
Range("D8:G16").Cells(1, 1)=1000
Will writes 1000 in the first cell (the upper left cell) of the selection D8:G16.

54
Excel VBA Introduction – Part 2

Make sure you understand that if you do not specify a range when using the Cells property, you are
referring to an absolute cell reference. Thus simply using:
Cells(1,1)=1000
This will always write 1000 in cell A1 whereas in the previous example it is writing in the upper-left cell
of the selection.
Suppose rng refers to the range D5:F7 on the right then:
rng.Cells(1, 1) Returns A
rng.Cells(3, 2) Returns F
The values used with the Cells property above are relative to the referenced range. If you do not
specify a range then it will use an absolute measurement.
Cells(1, 1) Refers to A1
Cells(3, 2) Refers to B3
You will most likely use variables to refer to a cell in a range instead of numeric values. This makes it
very easy to loop through every cell in a range and do something with each one. Here’s one example:
Sub Looping()
Dim i As Integer, j As Integer
For i = 1 To 5
For j = 1 To 5
Cells(i, j) = "R" & i & "C" & j
Next j
Next i
End Sub
This will fill the range A1:E5 with their respective row and column number. For example A1 will contain
R1C1 and E5 will contain R5C5.
Small assignment: Modify the above procedure so that it works from any selected cell instead of
always start at A1. Hint: You only need to add a single word at
the proper place.
Look at the following example:
Sub FillSelection()
Dim rng As Range
Dim intRows As Integer, intCols As
Integer
Dim r As Integer, c As Integer
Dim intValue As Integer

Set rng = Selection


intRows = rng.Rows.Count
intCols = rng.Columns.Count

For c = 1 To intCols
For r = 1 To intRows
intValue = intValue + 1
55
Excel VBA Introduction – Part 2

rng.Cells(r, c) = intValue
Next r
Next c
End Sub
Look at the first For – Next loop. The first time, the variable c will have the value 1. While c is equal
to 1 the variable r will have the values 1, 2, 3, 4 and 5. Can you see how this allows the loop to create
all the combination of cells in the first column of the selection? So the expression Cells(r, c) will
alternately refer to (1, 1), (1, 2) and so on to (1, 5). The same thing will happen when c will be equal to
2 and so on to last column of the selection.
You might come across Cells(1) in some sample code. This technique refers to the 1st cell in the
range at the upper-left corner. So Cells(2) would refer the second one across unless the range is a
single column.
Multiple Ranges Selections
The Excel object model offers a way to work with non-contiguous ranges selections. This is very handy
because you may wish to design your code with that type of selections. For example, you may want to
scan a range of cells for a value and continue in another range. This way, your code continues to run
and go about its business.
When you select two non-contiguous ranges you can access each range through the Areas collection.
The Areas collection contains one range object for each selected range. You normally access Areas
through the Selection property.
Select two ranges in a worksheet then type in the Immediate Window:
?Selection.Areas.Count
Now try:
?Selection.Areas(1).Address
Try also the value 2.
Now try the next one with the value 1 and 2 as well:
?Selection.Areas(1).Count
Since Areas is a collection of objects (Range objects) it is possible to loop through each range using a
For Each – Next loop (See Looping Through Items in a Collection on page 48).
Suppose you need to fill each selected ranges with the month of the year starting with Jan. The second
range will contain Feb and so on.
Sub FillMonths()
Dim rng As Range, i As Integer
For Each rng In Selection.Areas
i = i + 1
rng = MonthName(i, True)
Next
End Sub
Parts of the following procedure is exactly the same as the example on page 55 where we fill each cell
in a range with the cell number. In this example the code continues to fill the other selected ranges.
56
Excel VBA Introduction – Part 2

Sub FillAreas()
Dim rng As Range
Dim r As Integer, c As Integer
Dim intRows As Integer, intCols As Integer
Dim intValue As Integer

For Each rng In Selection.Areas


intCols = rng.Columns.Count
intRows = rng.Rows.Count
For c = 1 To intCols
For r = 1 To intRows
intValue = intValue + 1
rng.Cells(r, c) = intValue
Next r
Next c
Next rng
End Sub
All ranges in the selection are visited in the order that they were created and filled with a series of
consecutive numbers. If there is only one range then the Areas collection contains only one Range
object. This means that the above procedure will work with one or more range selected.

In Conclusion
Hopefully this handout did not intimidate you to the point that you think VBA is too difficult to learn. If
you work at it and search Google for more explanations you will find yourself creating small but useful
pieces of code. After a few months, you will look back at some of your ‘creations’ and wonder at how
clever you were to come up with such elegant solutions.
I often recommend that you start by creating useful functions because they are generally short and
can replace calculation you are already familiar with in your worksheets.
After a short while tackle a few simple macro procedures. If you do not know how to code a task in
Excel, create a macro doing that one task only. Look at the recorded macro and see how you can use
that code in your procedure adapting it to your need. You will find that often you can simply copy and
paste the recorded macro into your procedures.
Happy development,

Daniel Lamarche
ComboProjects

57
Excel VBA Introduction – Part 2

Continue Your Exploration


This section briefly introduced some of the most common objects used when programming Excel’s
object model.
Finding a good book tailored to your level is hard. I suggest that you start for a while to use Google and
search for Web pages containing many tutorials on Excel VBA. Here are a few of the one I particularly
like:
www.cpearson.com/excel/mainpage.aspx — do not let Chip impress you too much by some of his
more complex topics. I have found many good introduction articles there.
http://www.fontstuff.com/vba/index.htm — Martin Green is one of the guys able to make VBA
accessible to the layman. Many simple tutorials with a workbook to download.
http://www.tushar-mehta.com/excel/vba/index.htm — Series of short articles. Try ‘Beyond the
macro recorder’. A page full of simple definitions and short examples to strengthen you basic
understanding of Excel and VBA.
Sometimes I am asked if I could recommend a book that can help in learning more about Excel VBA. I
have both of these books and I like both.

For intermediate users. There Excellent intro to Excel VBA.


is also a version for Excel 2007 Unfortunately this edition does not
exist for Excel 2003.
Example oriented.
Tutorial oriented very well illustrated.
Confusing at first but it gets
better quickly. Strongly recommended.

Want a reliable source of information with heaps of reviews? Go to Amazon.com and type ‘Excel VBA
books’. See what hundreds of readers have said.

58

You might also like