Professional Documents
Culture Documents
Chapter 6
VBA for Modelers
DATA REFERENCED in
Examples
Properties of Range
Objects
1) Address: returns abs. address of a range
as a String.
Range("A2:F9").Address
2) Cells: returns value of a particular cell.
Range("A2:F9").Cells(3, 2)
Range("A2:F9").Cells(3, 2).Value
If range is a single row or column:
Range("A2:F2").Cells(3)
Properties of Range
Objects
NOTE: many objects (including Range)
have properties that are in fact
reference to objects down the
hierarchy.
Property Cells returned as object
Range("A2:F9").Cells(3,
2).Value
3) Column: returns index of the first column
in range.
Range(C2:F19").Column (Column A is
1, B is 2)
Properties of Range
Objects
4) CurrentRegion: Returns a Range object
that represents the current region. The
current region is a range bounded by any
combination of blank rows and blank
columns. (cursor must be in the region)
place cursor in A11 of interested range first to make it active
cell
Worksheets(Data).Range(A11).Select
select the entire current region surrounding active cell A11
ActiveCell.CurrentRegion.Select
(selects range
A11:B19)
ActiveCell.CurrentRegion.Address
Properties of Range
Objects
6) Font: returns a reference to the font of a
range.
Range("A2:F9").Font.Bold = True
7) Formula: returns or sets a formula in the
range.
Range("H20").Formula =
"=Power(B2,2)" sets formula
Range("H20").Formula returns cell formula
Properties of Range
Objects
8) FormulaR1C1: returns or sets formula in a
range in R1C1 notation. Very useful for formulas
copied down or across. It uses numbers to reference
both the rows and columns. Cell references are
displayed in terms of their relationship to the cell
Start
that contains the formula rather than
their
withactual
cell H20
Move 18 rows up,
position in the grid.
and 7 columns left to
Rows: up is -, down is +
Columns: left is -, right is +
R by itself means stay in the same
row
C by itself means stay in the same
column
Properties of Range
Objects
Properties of Range
Objects
9) HorizontalAlignment : returns or sets the
horizontal alignment for the specified object
(e.g. range). Three possible values are
xlRight, xlLeft, xlCenter.
Range(F20").HorizontalAlignment = xlLeft
10) Interior: returns a reference to the interior
of cells in range.
Range(F21").Interior.Color = vbRed
Properties of Range
Objects
11) Name: returns or sets the name
of the range
Range("B2:F9").Name = Scores"
Range("B2:F9").Name.Name
NOTE: A range can be named directly named on an
Excel sheet also
On Excel sheet, highlight range to be named
Right-click and select the option Define Name
Assign name in the dialog box
Click OK
Properties of Range
Objects
12)NumberFormat: returns or sets the format
code (as a String) for the range .
Range("F22").NumberFormat = "#,##0.00"
MsgBox Range("F22").NumberFormat
13)Offset: returns a reference relative to a range,
where the range is usually a single cell. This
property is very useful. See slides later.
Ranges(F5).Offset(-2, 3) start at F5, 2 rows up, 3
column right
Range(A1:F9).Sort
Key1:=Range(C1),
Order1:=xlAscending,
Header:=xlYes
column header not
part of sorting just scores
explicitly
intellisense list
b) Range(Range("A1").Offset(0,0),
Range("A1").Offset(3,3))
c) With Range(A1)
Range(.Offset(0,0), .Offset(3,3)).Select
These offsets must
be known in
End With
advance!
.End(xlDown).End(xlToRight)).Select
offsets need not be known; just go on till the end
is reached in both directions!
Range(Range("A1").Offset(0,0),
Range("A1").End(xlDown).End(xlToRight)).Select
EXAMPLES
Please study the 10 examples given
in the book on Pages 91-99.
You will need and use the concepts
from these examples extensively in
the semester!