week 3. assessed exercise 1 working with ranges
TRANSCRIPT
Week 3
Assessed Exercise 1 Working with ranges
Multiple representations• Different thing in different cases
Single cell
Collection of cells
The handle to the thing you want to work with
Many operations in Excel are range operations.• Select, copy, format, enter values or formulas
Important to automate range operations with VBA.
Ranges can be absolute or relative The Range object has properties and
methods. Use the Object Browser in the VBE to look up
any specific property or method. For more examples of ranges in VBA look at
the examples in the file Ranges.xls .
4
Use an address• Follow Range with an address in double quotes:
Range(“A1:A10”) Use a range name
• Follow Range with the range name in double quotes: Range(“Sales”)
Use the Offset property Use a Range object variable Use the End property (xlDown, xlUp,
xlToRight, xlToLeft)• You do not need to know the size of the range
5
You can refer to a range literally, using the Excel cell references in double quotes:
MsgBox Range("B1:F1").Address
MsgBox "The first score for the first employee is " & Range("B2").Value
You can apply a name to a range:• Range("B1:F1").Name = "ScoreNames“
Then use the range:• Range(“ScoreNames").Font.Bold = True
And remove names:• ActiveWorkbook.Names("ScoreNames").Del
ete
You can refer to cells within a named range:
Range("B2:F19").Name = "ScoreData“
MsgBox Range(“ScoreData”).Cells(2,3).Value
(returns the value in D3)
Select• Select method is like highlighting
Range(“B2:F5”).Select Activate
• Makes this cell “live” (only works on a single cell range, does not affect any “selection”) Range(“E3”).Activate
• Can activate a workbook or worksheet Worksheets(“Practice2”).Activate
As seen with macros can use “ActiveCell” and “Selection” as handles
Offset(rows,cols) is a property that allows you to refer to a cell offset from a range.
For example: Activecell.offset(rows,cols); or Selection.offset(rows,cols); or Range("D12").offset(rows,cols) Use with ActiveCell and with .End(xldown)
By using A1 as an anchor, all the offset property can be used where the actual range is unknown or variable
With Range("A1") Range(.Offset(0, 1), .Offset(0,1).End(xlToRight)).Name = _
"ScoreNames" Range(.Offset(1, 0), .Offset(1,0).End(xlDown)).Name = _
"EmployeeNumbers" Range(.Offset(1,
1), .Offset(1,1).End(xlDown).End(xlToRight)).Name = _ "ScoreData" End With
You can refer to columns or rows either in reference to the whole sheet or to a range or selection – for example, Columns(2).select is as follows:
The principle is the same for Rows. Rows(3) is the 3rd row in the sheet.
This sub formats the cells in the range ScoreNames. Using “With” avoids repetition
With Range("ScoreNames") .HorizontalAlignment = xlRight .Interior.ColorIndex = 5 With .Font .Bold = True .ColorIndex = 3 .Size = 16 End With
.EntireColumn.AutoFitEnd With
Formulas
Range(“B20”).Formula =“=AVERAGE(B2:B19)”Range(“B20”).FormulaR1C1 = "=AVERAGE(R[-18]C:R[-1]C)"
• Note R1C1 notation
• See Range9 for an example of applying a formula to several cells (like using autofill)
ActiveSheet.Rows(Rows.Count).SelectMsgBox Selection.Count
Sub Rangetest()MsgBox “The current selection is “ &
Selection.Rows.Count & _ “ rows high and” & Selection.Columns.Count & “ columns wide”
End SubWorksheets.count The number of worksheets in a workbook (file)
Workbooks.count The number of currently open files
Columns.count the number of columns in the current sheet
Rows.count the number of rows in the current sheet
Cells.count the number of cells in the current sheet
A region is a range of filled cells, bounded by empty cells. • The current region is where the currently selected cell is: it
includes all non-blank cells “touching” the current cell.
As long as you have one cell in the region selected, you can press Ctrl-Shift-8 (i.e., Ctrl-*) to select the region
This shortcut key corresponds to a line of code:Selection.CurrentRegion.Select
Use the wk03empscrnameranges file to complete the following tasks• Name cell A1 as Title• Name the headings in row 3 as Headings• Name the employee numbers in column A as EmpNumbers• Name the range of scores as Scores
With reference to the names above write a sub that performs the following tasks:• Make cell A1 bold and size 14• Make the headings on row 3 bold, italic and right aligned• Change the font colour for the employee numbers in column A
to blue• Change the background of the scores cells to light grey (Hint:
use the Interior property)• Enter the label “Averages” in cell A22 and make it bold• Enter a formula in B22 that calculates the average.• Copy the formula to C22:F22
For Each Loops
.Cells Used to refer to a particular cell.• Range(“A1:A10”).Cells(3) refers to the third cell in the
range• Range(“A1:A10”).Cells(3,2) refers to the cell in row 3,
column 2 .Font Returns a reference to the font of the range.
• Can change font properties • Range(“A1:A10”).Font.Bold = true
.Formula Returns or sets a formula in a range as a string.• Range(“A11”).Formula = “SUM(A1:A10)”
.Interior Returns a reference to the interior of a cell• Often used to change background colour• Range(“A1:A10”).Interior.Color = vbRed
.Name Returns or sets the name of a range.• Range(“A1:B10”).Name = “Sales”
19
.Value Returns or sets the value in the cell. • Default property for a Range object (can be omitted).• Usually used for a single-cell range.• unitPrice = Range(“A11”).Value or unitPrice =
Range(“A11”) .Offset Returns a reference relative to a range.
• The range is usually a single cell.• Very useful property, used constantly in most applications.• It takes two arguments: row offset and column offset.• Range(“A5”).Offset(2,3) = start in cell A5, go 2 rows
down, 3 columns to the right. • Use a positive offset to go down and a negative offset to go
up.• Use a positive offset to go to the right and a negative offset
to go to the left.
20
.Clear Deletes values and formatting from the range.
.ClearContents Deletes only values (and formulas).
.Copy Copies a range.• One optional argument: destination.• Range(“A1:B10”).Copy
Destination:=Range(“E1:F10”) .Paste Pastes clipboard contents to the range.
• Can have different arguments indicating how to paste.• Range(“E1:F10”).PasteSpecial
Paste:=xlPasteValue .Select Selects a range.
• Equivalent to highlighting a range in Excel.
21