Microsoft Office Tutorials and References
In Depth Information
Creating Simple Worksheet Functions
Creating Simple Worksheet Functions
Excel provides many worksheet functions, but sometimes you need something other than what’s
available by default. This tip provides a few examples of custom functions that can be used in
worksheet formulas.
Function procedures must be placed in a normal VBA module. If your workbook
doesn’t have any VBA modules, activate the Visual Basic Editor, select your workbook
from the Project list, and choose Insert➜Module.
Returning the user name
The following USERNAME function simply displays the name of the user — the name listed on the
General tab of the Options dialog box:
Function USERNAME()
USERNAME = Application.USERNAME
End Function
Here’s a worksheet formula that uses this function:
=USERNAME()
Determine whether a cell contains a formula
The following CELLHASFORMULA function accepts a single-cell argument and returns TRUE if
the cell has a formula:
Function CELLHASFORMULA(cell) As Boolean
‘ Returns TRUE if cell has a formula
CELLHASFORMULA = cell.Range(“A1”).HasFormula
End Function
If a multicell range argument is passed to the function, the function works with the upper left cell
in the range.
 
Search JabSto ::




Custom Search