Extract Date from text

eddie9874

New Member
Joined
Jan 26, 2018
Messages
3
Hi,

I need some help to extract date from texts, however the dates format are inconsistent and their position within the text is random but usually is toward the back part of the text. Examples as follows:

TextI would like to Extract
123456789 SMITH, C 40HR W/E 12.11.1212/11/2012
*4500273765 R TOM PE 05/11/2012 80 HRS05/11/2012
C INV 55662 BURN WE19/11/12 40 HRS19/11/2012
623111 MORRISON WE 26.11.2012 5 DAYS26/11/2012
24517983 - MONTH END 31-07-12

<tbody>
</tbody>
31/07/2012

<tbody>
</tbody>













I don't really know where to start, any help is appreciated.

Regards,

Eddie
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

I need some help to extract date from texts, however the dates format are inconsistent and their position within the text is random but usually is toward the back part of the text. Examples as follows:

TextI would like to Extract
123456789 SMITH, C 40HR W/E 12.11.1212/11/2012
*4500273765 R TOM PE 05/11/2012 80 HRS05/11/2012
C INV 55662 BURN WE19/11/12 40 HRS19/11/2012
623111 MORRISON WE 26.11.2012 5 DAYS26/11/2012
24517983 - MONTH END 31-07-12

<tbody>
</tbody>
31/07/2012

<tbody>
</tbody>
Do all of the dates in the cells always have a leading number for single digit months and days?
 
Upvote 0

Excel 2010
AB
2123456789 SMITH, C 40HR W/E 12.11.1212/11/12
3*4500273765 R TOM PE 05/11/2012 80 HRS05/11/2012
4C INV 55662 BURN WE19/11/12 40 HRS19/11/12 4
5623111 MORRISON WE 26.11.2012 5 DAYS26/11/2012
624517983 - MONTH END 31-07-1231/07/12
Sheet4
Cell Formulas
RangeFormula
B2=TRIM(SUBSTITUTE(SUBSTITUTE(IFERROR(MID(A2,SEARCH(".??.",A2,1)-2,10),"")&IFERROR(MID(A2,SEARCH("/??/",A2,1)-2,10),"")&IFERROR(MID(A2,SEARCH("-??-",A2,1)-2,10),""),".","/"),"-","/"))


Not sure what to expect on a larger sample (Row 4 has an unwanted digit at the end). Rick's regex solution will be better.
 
Last edited:
Upvote 0
Give this UDF a try...
Code:
[table="width: 500"]
[tr]
	[td]Function GetDate(ByVal S As String) As Date
  Dim DayPart As String, Parts() As String
  Parts = Split(Replace(Replace(Replace(S, " ", "x"), "-", "/"), ".", "/"), "/")
  DayPart = Right(Parts(UBound(Parts) - 2), 2)
  DayPart = Mid(DayPart, 2 + (Left(DayPart, 1) Like "#"))
  GetDate = DateSerial(Val(Parts(UBound(Parts))), Parts(UBound(Parts) - 1), DayPart)
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 GetDate just like it was a built-in Excel function. For example,

=GetDate(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
HI,

Another way :

=REPLACE(TEXT(REPLACE(REPLACE(MID(A2,IFERROR(SEARCH("??.??.",A2),IFERROR(SEARCH("??/??/",A2),SEARCH("??-??-",A2))),10),3,1,"/"),6,1,"/"),"dd/mm/yy"),9,2,"")


TextDate
123456789 SMITH, C 40HR W/E 12.11.1212/11/12
*4500273765 R TOM PE 05/11/2012 80 HRS05/11/12
C INV 55662 BURN WE19/11/12 40 HRS19/11/12
623111 MORRISON WE 26.11.2012 5 DAYS26/11/12
24517983 - MONTH END 31-07-1231/07/12

<tbody>
</tbody>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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