Remove Numbers from Alphanumeric String

D

Dave

Hi,

I have a value in Cell A of ABC123.

I want Cell B1 to contain the 123 from this cell.

Can anyone tell me the formula to enter in B1. I'm sure I've seen it
somewhere before but can't put my finger on it.

Thanks!
Dave
 
B

Bob Umlas

if it's always the 4th position, then =mid(A1,4,255). If you want it to be
numeric, then =1*mid(a1,4,255).
If the position is unknown but is always letters followed by numbers,
ctrl+shift+enter this:

=1*MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW($1:$20),1)),0),255)

Bob Umlas
Excel MVP
 
G

Gary''s Student

Try this UDF:

Function letteronly(r As Range) As String
letteronly = ""
If Application.WorksheetFunction.IsText(r.Value) Then
s = r.Value
For i = 49 To 57
s = Replace(s, Chr(i), "")
Next i
letteronly = s
End If
End Function
 
W

www.exciter.gr

You can try this Custom Function. Copy the code into the VBA window of
your file and then go to cell B1 and type =RemoveTexts(A1)

This function checks each character of your target cell and keeps only
numeric characters. Before returning the number, it actually converts
it to numeric (so 123 will be number, not text). For empty or text-
only cells, it will return zero.

Public Function RemoveTexts(Target As Range)
Dim t As String
For i = 1 To Len(Target.Value)
t = Mid(Target.Value, i, 1)
If IsNumeric(t) = True Then
RemoveTexts = RemoveTexts & t
End If
Next i
RemoveTexts = Val(RemoveTexts)
End Function
 
D

Dave

Thanks Farhad. This works, but my string does not always have 3 digits. Some
are 4 or 5 digits.
 
D

Dave

This returns #N/A

Bob Umlas said:
if it's always the 4th position, then =mid(A1,4,255). If you want it to be
numeric, then =1*mid(a1,4,255).
If the position is unknown but is always letters followed by numbers,
ctrl+shift+enter this:

=1*MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW($1:$20),1)),0),255)

Bob Umlas
Excel MVP
 
D

Dave

This doesn't seem to work.

www.exciter.gr said:
You can try this Custom Function. Copy the code into the VBA window of
your file and then go to cell B1 and type =RemoveTexts(A1)

This function checks each character of your target cell and keeps only
numeric characters. Before returning the number, it actually converts
it to numeric (so 123 will be number, not text). For empty or text-
only cells, it will return zero.

Public Function RemoveTexts(Target As Range)
Dim t As String
For i = 1 To Len(Target.Value)
t = Mid(Target.Value, i, 1)
If IsNumeric(t) = True Then
RemoveTexts = RemoveTexts & t
End If
Next i
RemoveTexts = Val(RemoveTexts)
End Function
 
R

Ragdyer

Probably because you *didn't* enter it the proper way.
It's an *array* formula!
--
Array formulas must be entered with CSE, <Ctrl> <Shift > <Enter>, instead of
the regular <Enter>, which will *automatically* enclose the formula in curly
brackets, which *cannot* be done manually.
You *must also* use CSE when revising the formula.

You can click in the cell containing the formula.
Then click in the formula bar, hold down
<Ctrl> and <Shift>
Then hit <Enter>
 
D

domenic22

Hi,

I have a value in Cell A of ABC123.

I want Cell B1 to contain the 123 from this cell.

Can anyone tell me the formula to enter in B1. I'm sure I've seen it
somewhere before but can't put my finger on it.

Thanks!
Dave

Try...

=REPLACE(A1,1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-1,"")

Note that the formula will return the number as a text value. To
return the number as a numerical value, add +0 at the end of the
formula.

Hope this helps!
 
G

Gord Dibben

Works for me.

Where did you store the code?

Copy and paste into a general module in your workbook.

Alt + F11 to go to Visual Basic Editor.

CTRL + r to open Project Explorer window.

Select your workbook/project and right-click>insert>module.

Paste the UDF into that module.

Alt + q to return to Excel sheet.

Enter the formula as shown.


Gord Dibben MS Excel MVP
 
W

www.exciter.gr: Custom Excel Applications!

Dave
just tried my solution again and it works fine.

Please follow the procedure step by step to make it work for you too:

1. Open your excel file (suppose its name is yourfile.xls)
2. Go to Tools/Macro/Visual Basic Editor
3. Right click on the line VBAProject(yourfile.xls) on the left list
and click on Insert Module
4. Paste the code into the white window on the right:
Public Function RemoveTexts(Target As Range)
Dim t As String
For i = 1 To Len(Target.Value)
t = Mid(Target.Value, i, 1)
If IsNumeric(t) = True Then
RemoveTexts = RemoveTexts & t
End If
Next i
RemoveTexts = Val(RemoveTexts)
End Function

5. Go back to your excel sheet, type ABC123 or anything else in Cell
A1
6. Type the formula: "=RemoveTexts(A1)" without the quotes into Cell
B1 and press enter
7. It should return the correct result

Good luck!
 
R

Ron Rosenfeld

Hi,

I have a value in Cell A of ABC123.

I want Cell B1 to contain the 123 from this cell.

Can anyone tell me the formula to enter in B1. I'm sure I've seen it
somewhere before but can't put my finger on it.

Thanks!
Dave

Assuming the digits are all contiguous:

B1:

=LOOKUP(9.99E+307,--MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},
A1&"0123456789")),ROW(INDIRECT("1:"&LEN(A1)))))

If the digits are not contiguous, you can use this UDF:

=======================
Option Explicit
Function Digits(str As String)
Dim re As Object
Const sPat As String = "\D"
Const sRes As String = ""

Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
Digits = re.Replace(str, sRes)
If IsNumeric(Digits) Then Digits = CDbl(Digits)
End Function
===========================

To enter the UDF, <alt-F11> opens the VBEditor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code above into the window that opens.

You can then use the formula =Digits(cell_ref) in any cell. e.g.

B1: =Digits(A1)
--ron
 
R

Ron Rosenfeld

Assuming the digits are all contiguous:

B1:

=LOOKUP(9.99E+307,--MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},
A1&"0123456789")),ROW(INDIRECT("1:"&LEN(A1)))))

If the digits are not contiguous, you can use this UDF:

=======================
Option Explicit
Function Digits(str As String)
Dim re As Object
Const sPat As String = "\D"
Const sRes As String = ""

Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
Digits = re.Replace(str, sRes)
If IsNumeric(Digits) Then Digits = CDbl(Digits)
End Function
===========================

To enter the UDF, <alt-F11> opens the VBEditor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code above into the window that opens.

You can then use the formula =Digits(cell_ref) in any cell. e.g.

B1: =Digits(A1)
--ron

Please note that the formulas I supplied return the digits as a value, and not
as a string. What this means is that leading 0's will get dropped.

If you require that the digits be returned as a string, then merely omit the
next to last line in the UDF (The line that starts with " If IsNumeric..."


--ron
 
D

Dave

Many thanks for this reply! It does now work. This solution would be very
useful for anyone trying to achieve a similar result!!

Cheers again
Dave
 
D

Dave

This formula does work when enterred as you mentioned. Can you please provide
some info on what this CSE method is and why it is used. It is new to me.

Thanks!
Dave
 
D

Dave

Is there a way to do something similar with IsText so that ABC is left, and
123 is removed?

Thanks!
Dave
 
R

Ron Rosenfeld

Is there a way to do something similar with IsText so that ABC is left, and
123 is removed?

Thanks!
Dave

You could use this UDF, which will return either Text or Digits depending on
the setting of the second (optional) argument:

==========================
Option Explicit
Function TextOrDigits(str As String, Optional Txt As Boolean = True)
Dim re As Object
Dim sPat As String
Const sRes As String = ""

If Txt = True Then
sPat = "\d"
Else
sPat = "\D"
End If

Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = sPat
TextOrDigits = re.Replace(str, sRes)
End Function
=================================

So to return just Text:

=TextOrDigits(A1)
or
=TextOrDigits(A1,True)

and to return just digits:

=TextOrDigits(A1,False)


--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top