Segregation specific numbers from given text or sentense

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi Team,

I hope anyone can help me on the formula or vba function to segregate the specific logic numbers from the given text ..here I mentioned input data and output and based on logic

Input Data

<tbody>
</tbody>
Output should be

<tbody>
</tbody>
Count of Digits

<tbody>
</tbody>
Logic Description

<tbody>
</tbody>
PO 9124818 /34 managed

<tbody>
</tbody>
9124818

<tbody>
</tbody>
7

<tbody>
</tbody>
Start from 9 and it will be there 7 digits

<tbody>
</tbody>
RO 31248184 11/9 taken from goods

<tbody>
</tbody>
31248184

<tbody>
</tbody>
8
Start from 3 and it will be there 8 digits

<tbody>
</tbody>
lOK 189897 should be fine at 9/123

<tbody>
</tbody>
189897

<tbody>
</tbody>
6
Start from 1 and it will be there 6 digits

<tbody>
</tbody>

<tbody>
</tbody>

Please let me know if this doable or not ...Thank you..
 
Hi Rick, I need those numbers as I have further requirement on those number ...looking for those numbers only (start from 9 followed by 7 digits, start from 3 followed by 8 numbers, start from 1 followed by 6 numbers including all starting numbers) other numbers not require...I hope answered your question ...
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi Eric, Many thanks for your response...looks it works many times...but still if anything followed further number for example "18988713/14/2007" if I want to try to get the 189887 but it will gives me 18988713

Is there any way to get rid of this as if its start from 1 it should only give 6 digits including starting number 1

The short answer is no. Even with VBA I couldn't tell how to parse that string. If there's no separation between numbers, I have no idea where to separate them. Between the 7 and 1? The 1 and 3? The 3 and /? This is what I meant when I said that inconsistent formatting makes it nearly impossible for reliable results. You, with your knowledge of what the numbers mean, might be able to make a better stab at manually picking the number, but I couldn't, let alone write a tool to do it automatically. I can't even think of a way to extract the number in most cases and flag the other cases for manual intervention.
 
Upvote 0
Hi Rick, I need those numbers as I have further requirement on those number ...looking for those numbers only (start from 9 followed by 7 digits, start from 3 followed by 8 numbers, start from 1 followed by 6 numbers including all starting numbers) other numbers not require...I hope answered your question ...
I think this UDF (user defined function) will do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Function GetNum(S As String) As Variant
  Dim X As Long
  GetNum = ""
  For X = 1 To Len(S) + 2
    If Mid("X" & S & "X", X, 8) Like "[!0-9]1#####[!0-9]" Then
      GetNum = Val(Mid(S, X, 7))
    ElseIf Mid("X" & S & "X", X, 10) Like "[!0-9]3#######[!0-9]" Then
      GetNum = Val(Mid(S, X, 9))
    ElseIf Mid("X" & S & "X", X, 9) Like "[!0-9]9######[!0-9]" Then
      GetNum = Val(Mid(S, X, 8))
    End If
    If Len(GetNum) Then Exit Function
  Next
End Function[/td]
[/tr]
[/table]

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use GetNum just like it was a built-in Excel function. For example,

=GetNum(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
This is an Excellent..!! Thanks for posting Rick Sir..

I think this UDF (user defined function) will do what you want...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function GetNum(S As String) As Variant
  Dim X As Long
  GetNum = ""
  For X = 1 To Len(S) + 2
    If Mid("X" & S & "X", X, 8) Like "[!0-9]1#####[!0-9]" Then
      GetNum = Val(Mid(S, X, 7))
    ElseIf Mid("X" & S & "X", X, 10) Like "[!0-9]3#######[!0-9]" Then
      GetNum = Val(Mid(S, X, 9))
    ElseIf Mid("X" & S & "X", X, 9) Like "[!0-9]9######[!0-9]" Then
      GetNum = Val(Mid(S, X, 8))
    End If
    If Len(GetNum) Then Exit Function
  Next
End Function[/TD]
[/TR]
</tbody>[/TABLE]

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use GetNum just like it was a built-in Excel function. For example,

=GetNum(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Thanks for post...Excellent solution..

This formula will find the longest number in the cell:

AB
1Input DataNumber
2PO 9124818 /34 managed9124818
3RO 31248184 11/9 taken from goods31248184
4lOK 189897 should be fine at 9/123189897
5PO /34 managed 91248189124818

<tbody>
</tbody>
Sheet6

Array Formulas
CellFormula
B2{=MAX(IFERROR(MID(SUBSTITUTE(A2," ","A"),ROW(INDIRECT("1:"&LEN(A2))),TRANSPOSE(ROW(INDIRECT("1:"&LEN(A2)))))+0,0))}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



There are still lots of cases where it might fail. Numbers over 15 digits will lose some due to Excel's 15-precision. If you have multiple numbers, it may pick the wrong one. Anytime you have inconsistent formatting, it's problematic trying to pick out the particular data you want.
 
Upvote 0
Hi Rick, Thank for your UDF code, it is working perfectly but where if the input like "18988712/14/2017" then I am not getting any output by using this function ...is there any reason? Thank you
 
Upvote 0
Give this a try. You may have to convert no-break spaces into normal spaces.

Excel Workbook
AB
2PO 9124818 /34 managed9124818
3RO 31248184 11/9 taken from goods31248184
4lOK 189897 should be fine at 9/123189897
518988712/14/2017189887
6PO /34 managed 91248189124818
Sheet1
 
Upvote 0
Hi Rick, Thank for your UDF code, it is working perfectly but where if the input like "18988712/14/2017" then I am not getting any output by using this function ...is there any reason? Thank you
That 1 is followed by 7 digits, not the 6 you said you were interested in getting the number for. Are you indicating you wanted the 1 plus the next 6 digits even when the 1 is followed by more than 6 digits?
 
Upvote 0
My apologies if my statement makes you confusion...here is the simplified summary...when start from 1 then I should get total 6 digits including number 1.....

If start from 9 then 7 digits (total) including starting number 9
If start from 3 then 8 digits (total) including starting number 3
If start form 1 then 6 digits (total) including starting number 1
 
Upvote 0
My apologies if my statement makes you confusion...here is the simplified summary...when start from 1 then I should get total 6 digits including number 1.....

If start from 9 then 7 digits (total) including starting number 9
If start from 3 then 8 digits (total) including starting number 3
If start form 1 then 6 digits (total) including starting number 1
I understand how to combine the two columns, what I am not sure of is how to list them... you message is not clear (to me) how you want the combined column values presented to you... in a single column ("a 1" in first cell, "a 2" in second cell, etc.) or in a single cell as a comma delimited list like your first post showed. Please clarify how the combined values should be shown to you.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

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