How do Abbreviate words in a string?

AndyD

Active Member
Joined
Nov 14, 2002
Messages
449
How do Abbreviate words in a string?

for example i need to abbreviate

London South East as "London SE"

London South West as "London SW"

London South as "London S" etc

Thanks
A
 
AndyD said:
sorry, i havent used UDF's in a while - can u remind me how to use, thanks
Insert the code into a standard module, and then use the function as normal on a spreadsheet.

just_jon said:
Hiya, TG: w/o UDF is pug-ugly, like --

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A6,"South East","SE"),"South West","SW"),"North East","NE"),"North West","NW"),"North","N"),"East","E"),"South","S"),"West","W")
That is exactly the same as what my "pug-ugly" UDF does.
:devilish:
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
AndyD said:
thanks all

i think al_b_cnu's is more along the right lines because a list would need to be updated when new records are added to our database

sorry, i havent used UDF's in a while - can u remind me how to use, thanks
A

Hi,
Sorry about the bug, try
Code:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String

I = InStr(Name, " ")
If I < 1 Then
    Abbreviate = Name
    Exit Function
End If

sResult = Left$(Name, I + 1)
sTemp = Right$(Name, Len(Name) - I)
I = InStr(sTemp, " ")
If I < 1 Then
    Abbreviate = sResult
    Exit Function
End If
Abbreviate = sResult & " " & Mid$(sTemp, I + 1, 1)

End Function

to insert, Alt-F11, Insert Module & paste code in.

HTH

Alan
 
Upvote 0
Hi,

A modified version which will cater for > 2 abbreviations & also cater for the special case of the string '(x)':

Code:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String

I = InStr(Name, " ")
If I < 1 Then
    Abbreviate = Name
    Exit Function
End If

sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
    sTemp = Right$(sTemp, Len(sTemp) - I)
    If Left$(sTemp, 1) = "(" Then
        If Mid$(sTemp & "***", 3, 1) = ")" Then
            sResult = sResult & " " & Left$(sTemp, 3)
        Else
            sResult = sResult & " " & Left$(sTemp, 1)
        End If
    Else
        sResult = sResult & " " & Left(sTemp, 1)
    End If
    I = InStr(sTemp, " ")
Loop
Abbreviate = sResult

End Function

Regards

Alan
 
Upvote 0
TommyGun said:
AndyD said:
sorry, i havent used UDF's in a while - can u remind me how to use, thanks
Insert the code into a standard module, and then use the function as normal on a spreadsheet.

just_jon said:
Hiya, TG: w/o UDF is pug-ugly, like --

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A6,"South East","SE"),"South West","SW"),"North East","NE"),"North West","NW"),"North","N"),"East","E"),"South","S"),"West","W")
That is exactly the same as what my "pug-ugly" UDF does.
:devilish:

Yeah, TG, but the UDF looks better... :biggrin:
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,293
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top