Find "****-***" in a text string

Grimm127

Board Regular
Joined
Aug 17, 2011
Messages
132
Hi All you Guru's,
I have a set of document numbers that are mixed in a with other wording in a cell.
I just want to pull from the text string the 7 digit combination with the dash in the middle.
Something that looks like this = 123-4567 or 552-7743. The doc number is set like that
However each text box will have a different set of numbers.
I was thinking of a find ***-**** to pull what I am looking for.

Any suggestions?

Thank you
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try

Book10
ABC
1
2aaaaaaa 123-4567 mmmmmmmmmm123-4567
3111111111 552-7743 222222222222552-7743
4Something that looks like this = 123-4567123-4567
5or 552-7743. The doc number is set like that552-7743
6
7
8
9
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=MID(A2,SEARCH("???-????",A2),8)
 
Upvote 0
Try

Book10
ABC
1
2aaaaaaa 123-4567 mmmmmmmmmm123-4567
3111111111 552-7743 222222222222552-7743
4Something that looks like this = 123-4567123-4567
5or 552-7743. The doc number is set like that552-7743
6
7
8
9
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=MID(A2,SEARCH("???-????",A2),8)
SOOOO very close! The problem is there is another set of values with alpha characters with a dash as well. It has to search for digits only.
 
Upvote 0
SOOOO very close! The problem is there is another set of values with alpha characters with a dash as well. It has to search for digits only.
For instance with your formula it will look at APR-2023 or Mar-June and return those values. I require only in that format in a numerical combination 123-4562, etc
 
Upvote 0
There will be defenitely shorter ways
but you can Try this for now


Race Query.xlsx
AB
1
2aaaaaaa 123-4567 mmmmmmmmmm123-4567
3111111111 552-7743 222222222222552-7743
4Something that looks like this = 123-4567123-4567
5or 552-7743. The doc number is set like that552-7743
6or aaa-aaaa 552-7743. The doc number is set like that552-7743
7or bbb-bbbb 552-7743. The doc number is set like that552-7743
8
9
Sheet2
Cell Formulas
RangeFormula
B2:B7B2=MID(IF(ISNUMBER(LEFT(MID(A2,SEARCH("???-????",A2),8))+0),MID(A2,SEARCH("???-????",A2),8),SUBSTITUTE(A2,MID(A2,SEARCH("???-????",A2),8),"")),SEARCH("???-????",IF(ISNUMBER(LEFT(MID(A2,SEARCH("???-????",A2),8))+0),MID(A2,SEARCH("???-????",A2),8),SUBSTITUTE(A2,MID(A2,SEARCH("???-????",A2),8),""))),8)
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

One option would be to employ a user-defined function like below. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function DocNum(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = "\d{3}\-\d{4}"
    If .Test(s) Then DocNum = .Execute(s)(0)
  End With
End Function

Grimm127.xlsm
AB
1
2aaaaaaa 123-4567 mmmmmmmmmm123-4567
3111111111 552-7743 222222222222552-7743
4Something that looks like this = 123-4567123-4567
5or 552-7743. The doc number is set like that552-7743
6or aaa-aaaa 552-7743. The doc number is set like that552-7743
7 
8or aaa-aaaa no doc number 
9or bbb-bbbb 552-7743. The doc number is set like that552-7743
Sheet1
Cell Formulas
RangeFormula
B2:B9B2=DocNum(A2)
 
Upvote 1
There will be defenitely shorter ways
but
Here are three samples:
HELP DESK OVERAGES PROCUREMENT-NOV22-166-2022
OwnBackup:Enhcd Sandbox Seed(Mar-22-Jan23)300-2022
Ownbackup:Enhanced Sandbox (Jun22-Feb23) 300-2022
 
Last edited by a moderator:
Upvote 0
What is your version of Excel actually? It would matter quite a bit.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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