You are on page 1of 22

Working with Ranges

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

5) EntireColumn: returns a reference


to a range consisting of all
participating column names in
entirety in the range.
Range("A2:F9").EntireColumn.Addre
ss

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

get to top-left corner


of range (i.e., cell
A2)
Move 11 rows up,
and 2 columns left to
get to bottom-right
corner of range (i.e.,
cell F9)
Then sum values in
range

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

14) Row, EntireRow: similar to Column and


EntireColumn.

EXCELs Custom Formatting

EXCELs Custom Formatting

EXCELs Custom Formatting

Methods of Range Objects


1) Clear: deletes everything from range (value, formula,
and formatting).
Range(A2:A10).Clear
2) ClearContents: deletes only the values and formulas.
3) Copy: copies a range and pastes it to a destination.
Range(A1:B10).Copy Destination:=Range(E1:F10)
4) PasteSpecial: this pastes according to specifications
spelled out in the arguments such as xlPastevalues.
Range(C3:D10).Copy
Range(F3:G10).PasteSpecial Paste:= xlPasteValues

Methods of Range Objects


5) Select: selects the range and highlights it
Range(A1:B10).Select
6) Sort: sorts the range. The way it sorts depends
on the arguments used such as Key (which
column to sort on), Order, Header (included or
not?)etc.

Range(A1:F9).Sort
Key1:=Range(C1),
Order1:=xlAscending,
Header:=xlYes
column header not
part of sorting just scores

Specifying Ranges with


VBA
Most common ways to reference a
range are:
1) Use an Address explicitly:
Range(A1) or Range(A1:F19)

2) Use a Range Name:


Naming range first, then use as:
Range(Sales)

3) Assign Range Name to a


Variable and use it:
Dim salesName as String

Specifying Ranges with VBA


4) Use a Range Object Variable
Dim salesRange as Range

declare a range obj.

explicitly

Set salesRange = Range(Sales)

can get help from

intellisense list

5) Use the Cells Property:


Range(C5:E15).Cells(4,2)

4 Rows down/2 Columns right

6) Use the Offset Property:


Range(A5).Offset(2,3)

start from A5; move 2 rows


down and 3 columns right; this takes you to cell D7 (counting 12
3 starts from next row or next column)

Specifying Ranges with VBA


7) Use Top/Left and Bottom/Right
Arguments:
Top-left
a) Range(Range(A1)
, Range(D4))
bottom-right

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!

Specifying Ranges with VBA


8) Use the End Property:
To select range A1:F19 filled with values:
With Range(A1)
Range(.Offset(0,0),
End With

.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

Advantage: You do not need to know the size of the


range.

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!

You might also like