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.

674<br />

Part VI: Developing Custom Worksheet Functions<br />

The functions in this section use the following statement:<br />

Application.Volatile True<br />

This statement causes the function to be reevaluated when the workbook is calculated.<br />

You’ll find, however, that these functions don’t always return the correct value. This is<br />

because changing cell formatting, for example, does not trigger Excel’s recalculation<br />

engine. To force a global recalculation (and update all the custom functions), press<br />

Ctrl+Alt +F9.<br />

The following function returns TRUE if its single-cell argument has bold formatting:<br />

Function ISBOLD(cell As Range) As Boolean<br />

‘ Returns TRUE if cell is bold<br />

Application.Volatile True<br />

ISBOLD = cell.Range(“A1”).Font.Bold<br />

End Function<br />

The following function returns TRUE if its single-cell argument has italic formatting:<br />

Function ISITALIC(cell As Range) As Boolean<br />

‘ Returns TRUE if cell is italic<br />

Application.Volatile True<br />

ISITALIC = cell.Range(“A1”).Font.Italic<br />

End Function<br />

Both of the preceding functions have a slight flaw: They return an error (#VALUE!) if the cell has<br />

mixed formatting. For example, it’s possible that only some characters in the cell are bold.<br />

The following function returns TRUE only if all the characters in the cell are bold. If the Bold<br />

property of the Font object returns Null (indicating mixed formatting), the If statement generates<br />

an error, and the function name is never set to TRUE. The function name was previously<br />

set to FALSE, so that’s the value returned by the function.<br />

Function ALLBOLD(cell As Range) As Boolean<br />

‘ Returns TRUE if all characters in cell are bold<br />

Dim UpperLeft As Range<br />

Application.Volatile True<br />

Set UpperLeft = cell.Range(“A1”)<br />

ALLBOLD = False<br />

If UpperLeft.Font.Bold Then ALLBOLD = True<br />

End Function

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

Saved successfully!

Ooh no, something went wrong!