11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

660<br />

Part VI: Developing Custom Worksheet Functions<br />

Cnt = Range(“A1:C300”).Count<br />

The Range property is also useful for referencing a single cell in a multicell range. For example,<br />

you may create a function that is supposed to accept a single-cell argument. If the user specifies<br />

a multicell range as the argument, you can use the Range property to extract the upper-left cell<br />

in the range. The following example uses the Range property (with an argument of “A1”) to<br />

return the value in the upper-left cell of the range represented by the cell argument.<br />

Function Square(cell as Range)<br />

CellValue = cell.Range(“A1”).Value<br />

Square = CellValue ^ 2<br />

End Function<br />

Assume that the user enters the following formula:<br />

=Square(C5:C12)<br />

The Square function works with the upper-left cell in C5:C12 (which is C5) and returns the value<br />

squared.<br />

Many Excel worksheet functions work in this way. For example, if you specify a multicell<br />

range as the first argument for the LEFT function, Excel uses the upper-left cell<br />

in the range. However, Excel is not consistent. If you specify a multicell range as the<br />

argument for the SQRT function, Excel returns an error.<br />

The Cells property<br />

Another way to reference a range is to use the Cells property. The Cells property accepts<br />

two arguments (a row number and a column number), and returns a single cell. The following<br />

statement assigns the value in cell A1 to a variable named FirstCell:<br />

FirstCell = Cells(1, 1).Value<br />

The following statement returns the upper-left cell in the range C5:C12:<br />

UpperLeft = Range(“C5:C12”).Cells(1,1)<br />

If you use the Cells property without an argument, it returns a range that consists of<br />

all cells on the worksheet. In the following example, the TotalCells variable contains<br />

the total number of cells in the worksheet:<br />

TotalCells = Cells.Count

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!