Extract text from alphanumeric strings. Formula, udf or code.

Alejandro Rodriguez

New Member
Joined
Jul 19, 2005
Messages
40
hi,

I have many registers with alphanumeric strings.
I want to extract only the text from these cells.

I have:
A2:FL9O7WER
A3:8T0AB765LE
A4:9FL7O8O4R4

and I need:
B2: FLOWER
B3: TABLE
B4: FLOOR

How to do this?. With a formula, udf or code?

Please help. Appreciate in advance your cooperation.
thanks,
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
UDF
1) Hit Alt + F11 to open VBE
2) go to [Insert] - [Module] then paste the code onto the right pane
3) hit Alt + F11 again to get back to Excel
Use in cell like
=AlphaNum(A1,True) '<- True for Alphabets, False for Numbers
Code:
Function AlphaNum(txt As String, Optional Alpha As Boolean = True) String
With CreateObject("VBScript.RegExp")
    .Pattern = IIf(Alpha, "\d+", "\D+")
    .Global = True
    AlphaNum = .replace(txt,"")
End With
End Function
 
Upvote 0
Hi there,

I'd say the udf way is the best.

Not sure if you already know how to do this, but the following five steps will put the code into a module from which you can run my udf like any other native excel formula - i.e. =TextOnly(A2):

1. Copy (Ctrl + C) my code
2. Open the VBA editor (Alt + F11)
3. From the Insert menu click Module
4. Paste (Ctrl + V) my code from step 1 above into the blank module
5. From the File menu click Close and Return to Microsoft Excel

HTH

Robert

Code:
Function TextOnly(rng As Range) As String

Dim intChrCnt As Integer
    For intChrCnt = 1 To Len(rng)
        If IsNumeric((Mid$(rng, intChrCnt, 1))) = False Then
            TextOnly = TextOnly & Mid$(rng, intChrCnt, 1)
        End If
    Next
    
End Function
 
Upvote 0
OOps
Rich (BB code):
Function AlphaNum(txt As String, Optional Alpha As Boolean = True) String
should be
Rich (BB code):
Function AlphaNum(txt As String, Optional Alpha As Boolean = True) As String
 
Upvote 0
I used the above code but i need a small variation. If there is no text found, i need it to return the word "cups". Thank you in advance.
 
Upvote 0
Hi jesma12,

Though it would be advisable to have started a new thread with a link back to this one, see how this goes:

Code:
Function AlphaExtract(txt As String, Optional varDefaultVal As Variant) As String

    'Use in a cell like any Excel function, i.e. _
    =AlphaExtract(A4,"cups")
    'will extract the text from A4 or return 'cups' _
    (though this can be text or a value) if there's no text in A4.

    With CreateObject("VBScript.RegExp")
        .Pattern = ("\d+")
        .Global = True
        AlphaExtract = .Replace(txt, "")
    End With
    
    If AlphaExtract = "" Then AlphaExtract = varDefaultVal
    
End Function

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,417
Members
448,895
Latest member
omarahmed1

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