Extract Just Dates From Text String

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
I am trying to extract dates from a cell like this: "Completed: 7/20/20 with primary contingency"

To do so, I used this formula with success:

=MID(A2,MIN(IFERROR(MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789",1)),LEN(A2)+1)),LOOKUP(1,0*MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),ROW(INDIRECT("1:"&LEN(A2)))) + 1 - MIN(IFERROR(MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789",1)),LEN(A2)+1)))

The problem is that not all text strings have just one set of numbers so this MID formula will not work. For example, a cell might say this: "Completed: 7/20/20 with primary contingency, G2 upper" or "Progress: U2, scheduled: 7/20/20 with primary contingency."

How do I get Excel to extract just the date from a cell and exclude all numbers that are not part of that date?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this:

varios 06ago2020.xlsm
AB
1
2Completed: 7/20/20 with primary contingency, G2 upper7/20/20
3Completed: 7/20/20 with primary contingency7/20/20
4Progress: U2, scheduled: 7/20/20 with primary contingency.7/20/20
xd
Cell Formulas
RangeFormula
B2:B4B2=TRIM(MID(A2,FIND("/",A2)-2,8))
 
Upvote 0
You can try

Book1
AB
1Progress: U2, scheduled: 7/20/20 with primary contingency.7/20/2002
Sheet5
Cell Formulas
RangeFormula
B1B1=AGGREGATE(15,6,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),{6,7,8,9,10})+0,1)


This finds the largest number in the string, which should be your date.

Edit:
I just noticed an error, the formula should be:
=AGGREGATE(14,6,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),{6,7,8,9,10})+0,1)
 
Last edited:
Upvote 0
Using Power Query/Get and Transform which is on the Data Tab, here is the Mcode

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Extracted Text Between Delimiters" = Table.TransformColumns(#"Changed Type", {{"Column1", each Text.BetweenDelimiters(_, ": ", " "), type text}})
in
    #"Extracted Text Between Delimiters"

If you are unfamiliar with PQ, then look at the links in my signature
 
Upvote 0
A VBA option.

Book2
DE
28Completed: 7/20/20 with primary contingency7/20/20
Sheet1
Cell Formulas
RangeFormula
E28E28=ExtractDate(D28)


VBA Code:
Function ExtractDate(d As String) As String
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "((?:\d{1,2})\/?)"
    Set m = .Execute(d)
    For Each x In m
        ExtractDate = ExtractDate & x
    Next x
End With
End Function
 
Upvote 0
If you have the year in 4 digits, then:

varios 06ago2020.xlsm
AB
1
2Completed: 7/20/20 with primary contingency, G2 upper7/20/20
3Completed: 7/20/2020 with primary contingency7/20/2020
4Progress: U2, scheduled: 7/20/20 with primary contingency.7/20/20
5Progress: U2, scheduled: 11/20/20 with primary contingency.11/20/20
6Progress: U2, scheduled: 11/5/2020 with primary contingency.11/5/2020
7Progress: U2, scheduled: 1/1/20 with primary contingency.1/1/20
8Progress: U2, scheduled: 1/1/2020 with primary contingency.1/1/2020
xd
Cell Formulas
RangeFormula
B2:B8B2=TRIM(LEFT(SUBSTITUTE(TRIM(MID(A2,SEARCH("/",A2)-2,LEN(A2)))," ",REPT(" ",99)),99))
 
Upvote 0
If you wanted to go the VBA route, the amended code below is more robust.

VBA Code:
Function ED(d As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "\b(?:\d{1,2}\/)+\d{2,4}\b"
    ED = .Execute(d)(0)
End With
End Function
 
Upvote 0
Sorry, one more question. When I run the first formula shown below, I get a date of 07/28/20. When I apply a filter, it does not show up under July but when I type in 7/28, it does show up under July in the filter. Any idea why this is happening?

=TRIM(MID(K3,FIND("/",K3)-2,8))
 
Upvote 0
The result of the formula is a text, it may have to be converted to date. try the following:

Dante Amor
AB
1
2Completed: 7/20/20 with primary contingency, G2 upper07/20/2020
3Completed: 7/3/20 with primary contingency07/03/2020
4Progress: U2, scheduled: 10/20/20 with primary contingency.10/20/2020
5Completed: 7/2/19 with primary contingency07/02/2019
Hoja3
Cell Formulas
RangeFormula
B2:B5B2=(SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")&"/"&TRIM(MID(A2,FIND("/",A2)-2,2))&"/"&TRIM(MID(A2,FIND("/",A2,FIND("/",A2)+1)+1,2)))+0
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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