Extract Capital Letters from Text in cell

Lorz

New Member
Joined
Oct 26, 2006
Messages
2
Hi,

In an attempt to extract area codes from a list of Towns/Cities I need to extract 1~3 capital letters from text in each cell

e.g.
CamBridge = CB
BoLton = BL
london East Central = EC

I gather, from searching previous posts, that recognising capital letters is tricky.

Any ideas would be gratefully received and save me from manually extracting hundreds.
 
Good afternoon Lorz

Try this in a normal module :

Code:
Function ExtractCap(Rng As Range)
Application.Volatile
ExtractCap = ""
For f = 1 To Len(Rng)
If Asc(Mid(Rng.Value, f, 1)) >= 65 And Asc(Mid(Rng.Value, f, 1)) <= 90 Then
ExtractCap = ExtractCap & Mid(Rng.Value, f, 1)
End If
Next f
End Function

And then use a formula such as this :

=ExtractCap(A1)

to show just the capital letters in A1.

HTH

DominicB
 
Upvote 0
This UDF should do the trick for you:

Code:
Function grabcaps(str As String) As String
Dim a As Integer: a = 1
Dim temp As String
    Do While a < (Len(str) + 1)
        If (Asc(Mid(str, a, 1))) = (Asc(UCase(Mid(str, a, 1)))) Then
            temp = temp & Mid(str, a, 1)
        End If
        a = a + 1
    Loop
    grabcaps = temp
End Function

Place the code in a standard module in the VBE Window (Alt + F11, Insert --> Module)

Then use as so in the worksheet

=grabcaps(A1)

Hope this helps
 
Upvote 0
A UDF alternative:

Code:
Function GetCaps(ByVal str As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "[^A-Z]"
    .Global = True
    GetCaps = .Replace(str, "")
End With
End Function
 
Upvote 0
Brilliant, thanks for the quick response guys!

DominicB's proved more effective in dealing with other obstacles further into my list such as hyphens.

Cheers!! ;-)
 
Upvote 0
I love this board.

So many ways to do these things, and always a better one than I come up with!!!


P.S Richard - I can't see any probs here at all
 
Upvote 0

Forum statistics

Threads
1,226,871
Messages
6,193,443
Members
453,799
Latest member
shanley ducker

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