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.

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

Figure 25-4 shows this function in use. Column A contains the text used as the first argument,<br />

and column B contains the text used as the second argument. Cell C1 contains this formula, which<br />

was copied down the column:<br />

=EXACTWORDINSTRING(A1,B1)<br />

Figure 25-4: A VBA function that determines if a particular word is contained in a string.<br />

Thanks to Rick Rothstein for suggesting this function — which is much more efficient<br />

than my original function.<br />

A workbook that demonstrates the EXACTWORDINSTRING function is available on the<br />

companion CD-ROM. The filename is exact word.xlsm.<br />

Does a cell contain text?<br />

A number of Excel’s worksheet functions are at times unreliable when dealing with text in a cell.<br />

For example, the ISTEXT function returns FALSE if its argument is a number that’s formatted as<br />

Text. The following CELLHASTEXT function returns TRUE if the cell argument contains text or<br />

contains a value formatted as Text:<br />

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

‘ Returns TRUE if cell contains a string<br />

‘ or cell is formatted as Text<br />

Dim UpperLeft as Range<br />

CELLHASTEXT = False<br />

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

If UpperLeft.NumberFormat = “@” Then<br />

CELLHASTEXT = True<br />

Exit Function<br />

End If<br />

If Not IsNumeric(UpperLeft.Value) Then<br />

CELLHASTEXT = True<br />

Exit Function<br />

End If<br />

End Function

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

Saved successfully!

Ooh no, something went wrong!