A custom function

The following is a custom function defined in a VBA module. This function, named Reverse, uses a single argument. The function reverses the characters in its argument (so that it reads backwards) and returns the result as a string.

If you use Excel 2000 or later, there is no real reason to create this function. You can just use the VBA StrReverse function.

Function Reverse(InString) As String ' Returns its argument, reversed

Dim StringLength as Integer , i as Integer Reverse = ""

StringLength = Len(InString) For i = StringLength To 1 Step -1

Reverse = Reverse & MidUnString, i, 1) Next i End Function

I explain how this function works later, in the "Analyzing the custom function" section.

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 Sheet or ThisWorkbook,they will not work in your formulas.

Was this article helpful?

0 0

Post a comment