Lookup number in text string

retboell

New Member
Joined
Dec 17, 2018
Messages
3
Hi,

I'm searching for a solution to lookup and return the number of a cell/text string. Example:

[TABLE="class: grid, width: 330"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Input[/TD]
[TD]Return[/TD]
[/TR]
[TR]
[TD]asd-1234567890123axbf[/TD]
[TD="align: right"]1234567890123[/TD]
[/TR]
[TR]
[TD]1234567890123_00sajs1[/TD]
[TD="align: right"]1234567890123[/TD]
[/TR]
[TR]
[TD]0-1234567890123sdfjquwe[/TD]
[TD="align: right"]1234567890123[/TD]
[/TR]
[TR]
[TD]asdasd-1-n1234567890123[/TD]
[TD="align: right"]1234567890123[/TD]
[/TR]
</tbody>[/TABLE]

The number will always start with 12345 (but the rest varies) and will always be 13 digits. Otherwise it can be mixed with other numbers and text in the cell (see example)

Help is much appreciated!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I think this should work, there might be better ways to do this though.

Try this:
Code:
=MID(A1,FIND(12345,A1),13)

Phil
 
Upvote 0
You are skipping some numbers while including others. What are the rules about that.?For example. what if the input string was:

[table="width: 500"]
[tr]
[td]asdasd-q98765321sdf-n1234567890123rtyg[/td]
[/tr]
[/table]

What should the output be?
 
Upvote 0
Here's a function that will return just numbers:
Code:
Function NumbersOnly(r As Range) As String
vOut = ""
vStr = r.Formula
For i = 1 To Len(vStr)
  If Mid(vStr, i, 1) >= "0" And Mid(vStr, i, 1) <= "9" Then
    vOut = vOut & Mid(vStr, i, 1)
  End If
Next
NumbersOnly = vOut
End Function
 
Upvote 0
The output for this would be 1234567890123 since the number I'm looking for is always 13 characters and always starts with 12345.
 
Upvote 0
Hi,

You already have a formula solution in Post # 2.
 
Upvote 0
Hi
Can I ask you what does line 3
Code:
 r.Formula
do? you did not assign any formula? can you please break it down?
Thank you.

Here's a function that will return just numbers:
Code:
Function NumbersOnly(r As Range) As String
vOut = ""
vStr = r.Formula
For i = 1 To Len(vStr)
  If Mid(vStr, i, 1) >= "0" And Mid(vStr, i, 1) <= "9" Then
    vOut = vOut & Mid(vStr, i, 1)
  End If
Next
NumbersOnly = vOut
End Function
 
Upvote 0
@lezawang
It's not really needed !!
You could use

Code:
vStr = r

OR

Code:
vStr = r.value

OR

Code:
vStr = r.formula
 
Upvote 0
Thank you very much for the help Michael M. I have another question please
Why his function is taking a range as a argument. Should be be a String?

Rich (BB code):
 Function NumbersOnly(r as String) As String 

Thank you once again.
 
Last edited:
Upvote 0
You might want to start your own thread if you have many questions, rather than "hi-jacking" someone else's thread...
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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