22
Working with Ranges Chapter 6 VBA for Modelers

Chapter 6 - Working With Ranges-F15

Embed Size (px)

DESCRIPTION

Working with Ranges-VBA

Citation preview

Page 1: Chapter 6 - Working With Ranges-F15

Working with RangesChapter 6

VBA for Modelers

Page 2: Chapter 6 - Working With Ranges-F15

DATA REFERENCED in Examples

Page 3: Chapter 6 - Working With Ranges-F15

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).ValueIf range is a single row or column:Range("A2:F2").Cells(3)

Page 4: Chapter 6 - Working With Ranges-F15

Properties of Range Objects

NOTE: many objects (including Range) have properties that are in fact reference to objects down the hierarchy.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)

Property Cells returned as object

Page 5: Chapter 6 - Working With Ranges-F15

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

Page 6: Chapter 6 - Working With Ranges-F15

5) EntireColumn: returns a reference to a range consisting of all participating column names in entirety in the range. Range("A2:F9").EntireColumn.Address

Page 7: Chapter 6 - Working With Ranges-F15

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

Page 8: Chapter 6 - Working With Ranges-F15

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 that contains the formula rather than their actual position in the grid. • Start with cell H20

• Move 18 rows up, and 7 columns left to 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

• 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

Page 9: Chapter 6 - Working With Ranges-F15

Properties of Range Objects

Page 10: Chapter 6 - Working With Ranges-F15

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

Page 11: Chapter 6 - Working With Ranges-F15

Properties of Range Objects

11) Name: returns or sets the name of the rangeRange("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

Page 12: Chapter 6 - Working With Ranges-F15

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").NumberFormat13) 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.

Page 13: Chapter 6 - Working With Ranges-F15

EXCEL’s Custom Formatting

Page 14: Chapter 6 - Working With Ranges-F15

EXCEL’s Custom Formatting

Page 15: Chapter 6 - Working With Ranges-F15

EXCEL’s Custom Formatting

Page 16: Chapter 6 - Working With Ranges-F15

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”).CopyRange(“F3:G10”).PasteSpecial Paste:= xlPasteValues

Page 17: Chapter 6 - Working With Ranges-F15

Methods of Range Objects

5) Select: selects the range and highlights itRange(“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

Page 18: Chapter 6 - Working With Ranges-F15

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 StringsalesName = Range(“Sales”).Name

Page 19: Chapter 6 - Working With Ranges-F15

Specifying Ranges with VBA

4) Use a Range Object VariableDim 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 1…2…3 starts from next row or next column)

Page 20: Chapter 6 - Working With Ranges-F15

Specifying Ranges with VBA

7) Use Top/Left and Bottom/Right Arguments:a) Range(Range(“A1”), Range(“D4”))

b) Range(Range("A1").Offset(0,0), Range("A1").Offset(3,3))

c) With Range(“A1”)

Range(.Offset(0,0), .Offset(3,3)).SelectEnd With These offsets must be

known in advance!

Top-left bottom-right

Page 21: Chapter 6 - Working With Ranges-F15

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(xlDown).End(xlToRight)).SelectEnd With

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.

offsets need not be known; just go on till the end is reached in both directions!

Page 22: Chapter 6 - Working With Ranges-F15

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!