Sunday, January 26, 2014

Function to reverse text string of variable length

UDF or custom function can work in the same way as built in function.
The example shown in picture reverse the text of string .VBA code define a function =Revtxt that when applied reverse the text of cell provided as input. To use this Function this must be stored in active workbook or workbook having this function must be open. You can also add this function to a xlam (add-in) file and make add-in available via excel add-in command bar.

Function RevText(txt As Range)
Application.Volatile
Dim strLength As Integer
Dim strCount As Integer
strLength = Len(txt)
ReverseText = ""
For strCount = strLength To 1 Step -1
RevText = RevText & _
Mid(txt.Value, strCount, 1)
Next strCount
End Function

\AppData\Roaming\Microsoft\AddIns is default location for add-in file

Open any excel file-press alt+f11 to open VBE editor-insert module and paste the code save this as an add-in file or macro enabled file.

In this example cell A1 having value as ABCD when we applies  Revtxt function on B1 output will be DCBA

ABCD  =RevText(A1)  DCBA 

No comments:

Post a Comment