VBA: Calculate Acronyms from Strings

October 4th, 2008 | Categories: Strings | Tags: , , ,
-->

The following function evaluates Acronyms from strings i.e it concatenates the first letter in every word in a string. E.g “trees are green” becomes “TAG”.

The routine traverses every character in a string and if it is a space then it takes the next character in the string. Before evaluating the string, it removes all trailing and duplicate spaces.

Function Acroymn (Original_String As String) As String
Dim Trimmed_String As String
Dim Length As Integer
Dim Pos As Integer
Trimmed_String = Application.WorksheetFunction.Trim(Original_String)

'work out the length of the string

Length = Len(Trimmed_String)

Acroymn = UCase(Left(Trimmed_String, 1))
For Pos = 2 To Length - 1
If (Mid(Trimmed_String, Pos, 1) = " ") Then
Acroymn = Acroymn & UCase(Mid(Trimmed_String, Pos + 1, 1))
End If
Next Pos
End Function

So for example :
Acroymn (“British Broadcasting Corporation”) gives BBC
Acroymn (“Funky”) gives F
Acroymn (“”) gives NULL

To download the .XLSM file from the article, click here

Can't get the tutorial to work for you? Need help with your code?
Get answers right away at our AE Excel Support Forums!
  1. October 8th, 2008 at 13:09
    Reply | Quote | #1

    Hi,

    This is a little faster.

    [vb]
    Function Acroymn2(Text) As String

    Dim strReturn As String
    Dim Item As Variant

    For Each Item In Split(Text, ” “)
    strReturn = strReturn & Left(Item, 1)
    Next
    Acroymn2 = UCase(strReturn)

    End Function
    [/vb]

    hopefully code tags work ;)