Question:
In Excel, how to extract a date from a text string?
John M.
2010-06-15 10:34:18 UTC
If I have a cell with text in it that includes a date (for example "will ship by 11/25/2010", what is the best way to extract the date into another cell (in date format)
Six answers:
garbo7441
2010-06-15 10:48:20 UTC
Edit 3: Revised to account for no date in string. If so, current date will be inserted:



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim i, LastRow

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Cells(i, "A").Value = "" And _

Application.CountA(Range("A" & i & ":" & "IV" & i)) > 0 Then

Cells(i, "B").Value = Format(Date, "mm/dd/yyyy")

End If

If Cells(i, "A").Value <> "" Then

dMark = Application.Find("/", Cells(i, "A"), 1)

If Len(Cells(i, "A")) < 12 Then

Cells(i, "B").Value = Cells(i, "A").Value

ElseIf IsError(Application.Find("/", Cells(i, "A"), 1)) Then

Cells(i, "B").Value = Format(Date, "mm/dd/yyyy")

Else

Cells(i, "B").Value = Format(Mid(Cells(i, "A"), _

dMark - 2, 10), "mm/dd/yyyy")

End If

End If

Next

End Sub





'==============





Edit2: Revised to handle an entire column:



Here is a VBA macro that will extract the date for every row in column A and insert it into the same row in column B.



If you wish to use different columns for your data, modify the column references thusly:



Change all "A" references to the column letter containing your text string, i.e. "F"



Change all "B" references to the column to insert the extracted date in, i.e. "H"



Then, copy the modified code to the clipboard:



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim i, LastRow

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Cells(i, "A").Value <> "" Then

dmark = Application.Find("/", Cells(i, "A"), 1)

If Len(Cells(i, "A")) < 12 Then

Cells(i, "B").Value = Cells(i, "A").Value

Else

Cells(i, "B").Value = Format(Mid(Cells(i, "A"), _

dmark - 2, 10), "mm/dd/yyyy")

End If

End If

Next

End Sub



Select the appropriate worksheet and right click the sheet tab.



Select 'View Code'



Paste the code into the sheet module editing area to the right.



Close the VBE and return to the worksheet.



The date will be extracted on any selection_change event.



It makes no difference where the date is in the string, nor the date structure. Works with 1 or 2 digit months and days.
?
2016-11-17 15:11:22 UTC
Excel Extract Text From String
?
2017-01-19 10:34:05 UTC
1
2016-03-02 05:37:08 UTC
The Text to Column feature can be applied more than once. After you apply it to your data and get the date, time and username into columns, select the column with the [MSG_xxx_yy]-MESSAGE- -''message'' - text text2 text3 text4 and click on Text to Column and use the - as the delimiter. Repeat as necessary. No formulae required.
Andrew L
2010-06-15 10:44:59 UTC
=DATEVALUE(RIGHT(A1,10))

Format the cell as date (right-click, Format cells, date)
CDV
2010-06-15 18:10:50 UTC
A1 your text, B1 =FIND("/201",A1), C1 =FIND(" ",A1,C1-6)

D1 =VALUE(MID(A1,D1+1,C1-D1+5)) or

D1 =IF(ISERROR(VALUE (MID(A1,D1+1,C1-D1+5))), TODAY(),

VALUE(MID(A1,D1+1,C1-D1+5)))



Solution good till 2020 then change the formula in B1


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...