A custom function

The following is a custom function defined in a VBA module. This function, named RemoveVowels, uses a single argument. The function returns the argument, but with all the vowels removed.

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

This certainly isn't the most useful function I've written, but it demonstrates some key concepts related to functions. I explain how this function works later, in the "Analyzing the custom function" section.

Caution When you create custom functions that will be used in a worksheet formula, make sure that the code resides in a normal VBA module. If you place your custom functions in a code module for a userForm, a Sheet, or ThisWorkbook, they will not work in your formulas.

0 0

Post a comment