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.

Chapter 25: VBA Custom Function Examples 675<br />

The following FILLCOLOR function returns an integer that corresponds to the color index of the<br />

cell’s interior (the cell’s fill color). If the cell’s interior is not filled, the function returns –4142. The<br />

ColorIndex property ranges from 0 to 56.<br />

Function FILLCOLOR(cell As Range) As Long<br />

‘ Returns a value corresponding to<br />

‘ cell’s interior color<br />

Application.Volatile True<br />

FILLCOLOR = cell.Range(“A1”).Interior.ColorIndex<br />

End Function<br />

If a cell is part of a table that uses a style, the FILLCOLOR function does not return the<br />

correct color. Similarly, a fill color that results from conditional formatting is not<br />

returned by this function.<br />

The following function returns the number format string for a cell:<br />

Function NUMBERFORMAT(cell As Range) As String<br />

‘ Returns a string that represents<br />

‘ the cell’s number format<br />

Application.Volatile True<br />

NUMBERFORMAT = cell.Range(“A1”).NumberFormat<br />

End Function<br />

If the cell uses the default number format, the function returns the string General.<br />

Determining a Cell’s Data Type<br />

Excel provides a number of built-in functions that can help determine the type of data contained<br />

in a cell. These include ISTEXT, ISLOGICAL, and ISERROR. In addition, VBA includes functions<br />

such as ISEMPTY, ISDATE, and ISNUMERIC.<br />

The following function accepts a range argument and returns a string (Blank, Text, Logical, Error,<br />

Date, Time, or Value) that describes the data type of the upper-left cell in the range:<br />

Function CELLTYPE(cell As Range) As String<br />

‘ Returns the cell type of the upper-left<br />

‘ cell in a range<br />

Dim UpperLeft As Range<br />

Application.Volatile True<br />

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

Select Case True

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

Saved successfully!

Ooh no, something went wrong!