You are on page 1of 5

Actually, the function works like any built-in worksheet function.

You
can insert it in a formula by choosing FormulasFunction
LibraryInsert Function or by clicking the Insert Function Wizard
icon to the left of the formula bar. Either of these actions displays the
Insert Function dialog box. In the Insert Function dialog box, your
custom functions are located, by default, in the User Defined category.
You can also nest custom functions and combine them with other
elements in your formulas. For example, the following formula nests
the REMOVEVOWELS function inside Excel's UPPER function. The
result is the original string (sans vowels), converted to uppercase.
=UPPER(REMOVEVOWELS(A1))

Using the function in a VBA procedure


In addition to using custom functions in worksheet formulas, you can
use them in other VBA procedures. The following VBA procedure,
which is defined in the same module as the custom
REMOVEVOWELS function, first displays an input box to solicit text
from the user. Then the procedure uses the VBA built-in MsgBox
function to display the user input after the REMOVEVOWELS
function processes it (see Figure 8-2). The original input appears as
the caption in the message box.
Sub ZapTheVowels()
Dim UserInput as String
UserInput = InputBox(Enter some text:)
MsgBox REMOVEVOWELS(UserInput),
vbInformation, UserInput
End Sub

Figure 8-2 shows text entered into an input box, and the result

displayed in a message box .

Figure 8-2: Using a custom function in a VBA procedure.

Analyzing the custom function


Function procedures can be as complex as you need them to be. Most
of the time, they're more complex and much more useful than this
sample procedure. Nonetheless, an analysis of this example may help
you understand what is happening.
Here's the code, again:
Function REMOVEVOWELS(Txt) As String
Removes all vowels from the Txt argument
Dim i As Long
REMOVEVOWELS =
For i = 1 To Len(Txt)
If Not UCase(Mid(Txt, i, 1)) Like
[AEIOU] Then
REMOVEVOWELS = REMOVEVOWELS & Mid(Txt,
i, 1)
End If
Next i

End Function

Note that the procedure starts with the keyword Function, rather than
Sub, followed by the name of the function (REMOVEVOWELS). This
custom function uses only one argument (Txt), enclosed in
parentheses. As String defines the data type of the function's return
value. Excel uses the Variant data type if no data type is specified.
The second line is an optional comment that describes what the
function does. This line is followed by a Dim statement, which
declares the variable (i) used in the procedure as type Long.

I use the function name as a variable and initialize it


to an empty string. When a function ends, it always
returns the current value of the variable that
corresponds to the function's name.
The next five instructions make up a For-Next loop. The procedure
loops through each character in the input and builds the string. The
first instruction in the loop uses VBA's Mid function to return a single
character from the input string and converts this character to
uppercase. That character is then compared to a list of characters by
using Excel's Like operator. In other words, the If clause is true if the
character isn't A, E, I, O, or U. In such a case, the character is
appended to the REMOVEVOWELS variable.
When the loop is finished, REMOVEVOWELS consists of the input
string with all vowels removed. This string is the value that the
function returns.

The procedure ends with an End Function statement.


Keep in mind that you can do the coding for this function in a number
of different ways. Here's a function that accomplishes the same result
but is coded differently:
Function REMOVEVOWELS(txt) As String
Removes all vowels from the Txt argument
Dim i As Long
Dim TempString As String
TempString =
For i = 1 To Len(txt)
Select Case ucase(Mid(txt, i, 1))
Case A, E, I, O, U
Do nothing
Case Else
TempString = TempString & Mid(txt,
i, 1)
End Select
Next i
REMOVEVOWELS = TempString
End Function

In this version, I used a string variable (TempString) to store the


vowel-less string as it's being constructed. Then, before the procedure
ends, I assigned the contents of TempString to the function's name.
This version also uses a Select Case construct rather than an If-Then
construct.

Both versions of this function are available at this


book's website. The file is named remove
vowels.xlsm.

What custom worksheet functions


cant do
When you develop custom functions, it's important to understand a key distinction
between functions that you call from other VBA procedures and functions that you use in
worksheet formulas. Function procedures used in worksheet formulas must be passive.
For example, code in a Function procedure can't manipulate ranges or change things on
the worksheet. An example can help make this limitation clear.
You may be tempted to write a custom worksheet function that changes a cell's
formatting. For example, it may be useful to have a formula that uses a custom function to
change the color of text in a cell based on the cell's value. Try as you might, however,
such a function is impossible to write. No matter what you do, the function won't change
the worksheet. Remember, a function simply returns a value. It can't perform actions with
objects.
That said, I should point out one notable exception. You can change the text in a cell
comment by using a custom VBA function. I'm not sure if this behavior is intentional or if
it's a bug in Excel. In any case, modifying a comment via a function seems to work
reliably. Here's the function:

Function MODIFYCOMMENT(Cell As Range, Cmt As String)


Cell.Comment.Text Cmt
End Function
Here's an example of using this function in a formula. The formula replaces the comment
in cell A1 with new text. The function won't work if cell A1 doesn't have a comment.

=MODIFYCOMMENT(A1,Hey, I changed your comment)

Function Procedures
A Function procedure has much in common with a Sub procedure.
(For more information on Sub procedures, see Chapter 7.)

You might also like