11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

666<br />

Part VI: Developing Custom Worksheet Functions<br />

You can also write this function without using an If-Then-Else construct. In the following<br />

function, the expression to the right of the equal sign returns either TRUE or FALSE — and this<br />

value is returned by the function:<br />

Function CELLISHIDDEN(cell)<br />

CELLISHIDDEN = cell.EntireRow.Hidden Or _<br />

cell.EntireColumn.Hidden<br />

End Function<br />

The Set keyword<br />

An important concept in VBA is the ability to create a new Range object and assign it to a variable<br />

— more specifically, an object variable. You do so by using the Set keyword. The following<br />

statement creates an object variable named MyRange:<br />

Set MyRange = Range(“A1:A10”)<br />

After the statement executes, you can use the MyRange variable in your code in place of the<br />

actual range reference. Examples in subsequent sections help to clarify this concept.<br />

Creating a Range object is not the same as creating a named range. In other words, you<br />

can’t use the name of a Range object in your worksheet formulas.<br />

The Intersect function<br />

The Intersect function returns a range that consists of the intersection of two other ranges.<br />

For example, consider the two ranges selected in Figure 24-2. These ranges, D3:D10 and B5:F5,<br />

contain one cell in common (D5). In other words, D5 is the intersection of D3:D10 and B5:F5.<br />

The following Function procedure accepts two range arguments and returns the count of the<br />

number of cells that the ranges have in common:<br />

Function CELLSINCOMMON(rng1, rng2)<br />

Dim CommonCells As Range<br />

On Error Resume Next<br />

Set CommonCells = Intersect(rng1, rng2)<br />

If Err.Number = 0 Then<br />

CELLSINCOMMON = CommonCells.CountLarge<br />

Else<br />

CELLSINCOMMON = 0<br />

End If<br />

End Function

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

Saved successfully!

Ooh no, something went wrong!