Search for number in a text string

CarlCarlos

New Member
Joined
Jan 12, 2011
Messages
31
Dear all,

I have a list of character strings (examples below) which are likely to have a digit or two in them (1 to 12)

I need a formula that can identify the numbers within that string, is that possible?

i.e.

afafafaf1fafaf
awefrrd6ewtwtg
bb12rrwa

the strings are differing in length and the number could be 1 or two digits and do not necessarily reside in the same place for each string.
 
I guess I was inspired! :)

... I made a slight mod tho

Code:
Function GetNumber(s As String, Optional lInd As Long = 1)
That way I can omit the second argument and it'll default to extracting the first number.


Thanks Weaver, for the improvement. It makes all the sense, that's how it should have been from the start.


...
Can you explain how the =-LOOKUP(0,-MID(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789")),{1,2})) works?

This formula could be handy for me in the future.

Biz

Hi Biz

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

Since in this case we have a number with either 1 or 2 digits, the Mid() formula extracts 2 strings, just the first digit, or the first digit and the following character.

Ex.: "ab12cd": you get {"1","12"}
Ex.: "ab1cde": you get {"1","1c"}

by applying a minus before the array, you convert its values to number, or to error, if it's not possible to convert.

Ex.: "ab12cd": you get =-LOOKUP(0,{-1,-12})
Ex.: "ab1cde": you get =-LOOKUP(0,{-1,#VALUE!})

As you know, when the lookup value is bigger than any of the values in the lookup array, the Lookup() function returns the last value in the array. We use zero as the lookup value, bigger than any negative number, and so we get the last value, -12 in the first case and -1 in the second.

Notice that it's equivalent to

=LOOKUP(100,0+MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2}))

In this case we also know that 100 is bigger than any number with 2 digits.

HTH
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I guess I was inspired! :)




Thanks Weaver, for the improvement. It makes all the sense, that's how it should have been from the start.




Hi Biz

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

Since in this case we have a number with either 1 or 2 digits, the Mid() formula extracts 2 strings, just the first digit, or the first digit and the following character.

Ex.: "ab12cd": you get {"1","12"}
Ex.: "ab1cde": you get {"1","1c"}

by applying a minus before the array, you convert its values to number, or to error, if it's not possible to convert.

Ex.: "ab12cd": you get =-LOOKUP(0,{-1,-12})
Ex.: "ab1cde": you get =-LOOKUP(0,{-1,#VALUE!})

As you know, when the lookup value is bigger than any of the values in the lookup array, the Lookup() function returns the last value in the array. We use zero as the lookup value, bigger than any negative number, and so we get the last value, -12 in the first case and -1 in the second.

Notice that it's equivalent to

=LOOKUP(100,0+MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2}))

In this case we also know that 100 is bigger than any number with 2 digits.

HTH

Thank u mate again.

Biz
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,936
Members
452,949
Latest member
beartooth91

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