Tutorial 2
Tutorial 2
This page contains the 2nd lesson on the Excel VBA Basic Tutorial series. It
covers topics in the most used Excel objects and collections. Beginners in VBA
programming are encouraged to go through the 1st lessons in this series if they
had not already done so. This document contains information about the following
topics.
Objects and Collections
Workbook and Worksheet Object
Range Object and Cells Property
Methods and Property
Assigning Object Variables and Using Named Argument
Microsoft Support site or the Excel VBA Help section on your computer contains
comprehensive examples on most the issues covered on this page. For more
information, please refer to them.
The following figure shows all the objects mentioned. The workbook (Excel file)
is currently Book3.xls. The current worksheet is Sheet1 as the Sheet Tab
indicated. Two ranges are selected, range B2 and B7:B11.
1
Return to Top of Page
A workbook is the same as an Excel file. The Workbook collection contains all
the workbooks that are currently opened. Inside of a workbook contains at least
one worksheet. In VBA, a worksheet can be referenced as followed:
Worksheets("Sheet1")
Worksheets(1)
Sheets is a collection of worksheets and chart sheets (if present). A sheet can
be indexed just like a worksheet. Sheets(1) is the first sheet in the workbook.
To refer sheets (or other objects) with the same name, you have to qualify the
object. For example:
2
Workbooks("Book1").Worksheets("Sheet1")
Workbooks("Book2").Worksheets("Sheet1")
If the object is not qualified, the active or the current object (for example
workbook or worksheet) is used.
The sheet tab on the buttom the spreadsheet (worksheet) shows which sheet is
active. As the figure below shows, the active sheet is "Sheet1" (show in bold
font and white background).
* You can change the color of the sheet tabs by right click the tab, choose Tab
Color, then select the color for the tab.
The sub routine below shows the name of each sheet in the current opened
workbook. You can use For Each...Next loop to loop throgh the Worksheets
collection.
Sub ShowWorkSheets()
Dim mySheet As Worksheet
For Each mySheet In Worksheets
MsgBox mySheet.Name
Next mySheet
End Sub
Worksheets("Sheet2").Range("A1:B5") = "AB"
3
:
Note that, Worksheets.Range("A1", "B5") = "AB" will yield the same result as the
above example.
Range object has a Cells property. This property is used in every VBA projects
on this website (very important). The Cells property takes one or two indexes as
its parameters.
For example,
where row is the row index and column is the column index.
ActiveSheet.Range.Cells(1,1)
Range.Cells(1,1)
Cells(1,1)
Cells(12) = "XYZ"
4
Range("B1:F5").cells(12) = "ZYZ"
* The small gray number on each of the cells is just for reference purpose only.
They are used to show how the cells are indexed within the range.
Here is a sub routine that prints the corresponding row and column index from A1
to E5.
Sub CellsExample()
For i = 1 To 5
For j = 1 To 5
Cells(i, j) = "Row " & i & " Col " & j
Next j
Next i
End Sub
Range object has an Offset property that can be very handy when one wants to
move the active cell around. The following examples demostrate how the Offset
property can be implemented (assume the current cell before the move is E5):
5
ActiveCell.Offset(0,-3) = 1 Place a "1" three
columns to the left of E5 (on B5)
Workbooks.Close
Close method close the active workbook
Workbooks.Count
Count property returns the number of workbooks that are currently opened
Some objects have default properties. For example, Range's default property is
Value.
The following yields the same outcome.
Here are examples on how to set and to get a Range property value:
The following sets the value of range A1 or Cells(1,1) as "2005". It actually
prints "2005" on A1.
Range("A1").Value = 2005
X = Range("A1").Value
6
Method can be used with or without argument(s). The following two examples
demostrate this behavior.
Worksheets("Sheet").Column("A:B").AutoFit
Methods That Take Arguments:
Worksheets("Sheet1").Range("A1:A10").Sort _
Worksheets("Sheet1").Range("A1")
Worksheets("Sheet1").Range("A1") is the Key (or column) to sort by.
Sometime a method takes more than one argument. For example, the Open
method for the Workbook
object, takes 12 arguments. To open a workbook with password protection, you
would need to write the following code:
Since this method takes so many arguments, it is easy to misplace the password
argument. To
overcome this potential problem, one can use named arguments like the following
example:
You can also assign an object to an object variable using the Set Statement.
For example: