Extract Date from field with "unformatted date" in

aka_krakur

Active Member
Joined
Jan 31, 2006
Messages
438
I have a spreadsheet that has a column CLOSE_JUSTIFICATION.
In this field the Analyst will type in information as to why the Incident is "reopened" and will include a date using a non-formatted date format. (ie. 1-3-07, 12-2-06, etc).
Here is just a section of the spreadsheet (Column A & Column B & Column C which I'm hoping to find a way to write a formula to extract the date(s) from Column A into B & C:
Book2
ABCD
1CLOSE_JUSTIFICATIONReopenDate1ReopenDate2
2Reopenedforadditionalinformation.//pv6-7-05Reopenedtoadddocumentation.//pv1-3-07
3
4Reopenedforadditionalinformation.//PRP1-24-07
5Reopenedforadditionalinformation.//PRP1-24-07
6Reopenedforadditionalinformation.//PRP11-7-06
7
8Reopenedforadditionalinformation.//PRP10-27-06
9Reopenedforadditionalinformation.//pv12-20-06
Sheet1
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Found Macro that extracts date (GetDate)

I found a macro that extracts date from text string like I need:

macro:
Code:
Function GetDate(strInput As String) As Date
Dim DateFormat() As String
Dim intDateLength As Integer
Dim intMaxFormat As Integer
Dim intFrmtCtr As Integer
Dim intPosition As Integer

intMaxFormat = 6
ReDim DateFormat(1 To intMaxFormat)

DateFormat(1) = "*##[-/]##[/-]####*"
DateFormat(2) = "*#[-/]##[-/]####*"
DateFormat(3) = "*##[-/]#[-/]####*"
DateFormat(4) = "*##[-/]##[-/]##*"
DateFormat(5) = "*#[-/]##[-/]##*"
DateFormat(6) = "*#[-/]#[-/]##*"

GetDate = Now

For intFrmtCtr = 1 To intMaxFormat

If strInput Like DateFormat(intFrmtCtr) Then


intDateLength = Len(DateFormat(intFrmtCtr)) - 8


strInput = Replace(strInput, " ", "")



For intPosition = 1 To Len(strInput)

If Mid(strInput, intPosition, intDateLength) Like DateFormat(intFrmtCtr) Then
GetDate = DateValue(Mid(strInput, intPosition, intDateLength))
Exit Function
End If

Next intPosition

End If

Next intFrmtCtr

End Function

I need to see if someone can help me re-write this macro to "loop" or something when there are multiple dates in the text string.
Possibly instead of typing =getdate(A2) into B2 have it be a macro that I have to run which will loop and populate cells B2: as many columns as needed to extract all dates from A2

Here's an example where there are multiple dates in the text string in A2
Reopened for additional information. //PRP 11-20-06
Reopened to add adjudication form. //PRP 11-30-06
Reopened for adjudication form. //PRP 12-8-06"

Currently when I type =getdate(A2) into B2, it returns the first date it finds: 11/20/06.

I would like the macro to loop and fill B2, C2, & D2 with 11/20/06, 11/30/06, and 12/08/06, respectfully.

Any suggestions?
 
Upvote 0
Can anyone help me with this? I have tried all this weekend to figure out how to extract multiple dates out of the text string (in one cell).
I cannot figure out how to manipulate the macro to perform this function.

Can one of you experts help me out.

Thanks
 
Upvote 0
Try this formula in cell C2.
Code:
=IF(LEN(A2)<60,"",RIGHT(A2,LEN(A2)-FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)-LEN(SUBSTITUTE(A2," ","")))))*1)
This only gets the last date if the length of the text in A2 is larger than 60 characters.
 
Upvote 0
That worked great; now I still need to figure out when there are 3 (or even more) how to get all of these dates extracted out.
Other problem I found was there was an incident where my =getdate(A2) formula returned results of the last date (instead of the 1st date). So the formula you posted also grabbed the last date and the 1st date was left without being extracted out like I need.

Any more suggestions.

I still feel as if there should be a way to edit the macro and instead of it being a =getdate formula based ran macro, it could be executed with a button or some shortcut (easily done) and it extracts as many dates as it can fine based on the formats listed in the macro) and extracts them into the last empty column it finds in that row.

I'm just not familiar enough with vb code to write or edit this existing one.

PLEASE HELP!!!
 
Upvote 0
Okay, I'm getting a lot closer to getting a macro to extract out dates from a text string.

Here's two different macros that are getting me there; however, I need to get some of the 2nd macro into the 1st so it more or less loops and grabs all dates (instead of just one)
here they are:

Code:
Function GetDate(strInput As String) As Date 
Dim DateFormat() As String 
Dim intDateLength As Integer 
Dim intMaxFormat As Integer 
Dim intFrmtCtr As Integer 
Dim intPosition As Integer 

intMaxFormat = 6 
ReDim DateFormat(1 To intMaxFormat) 

DateFormat(1) = "*##[-/]##[/-]####*" 
DateFormat(2) = "*#[-/]##[-/]####*" 
DateFormat(3) = "*##[-/]#[-/]##*" 
DateFormat(4) = "*##[-/]##[-/]##*" 
DateFormat(5) = "*#[-/]##[-/]##*" 
DateFormat(6) = "*#[-/]#[-/]##*" 



GetDate = Now 

For intFrmtCtr = 1 To intMaxFormat 

If strInput Like DateFormat(intFrmtCtr) Then 


intDateLength = Len(DateFormat(intFrmtCtr)) - 8 


strInput = Replace(strInput, " ", "") 



For intPosition = 1 To Len(strInput) 

If Mid(strInput, intPosition, intDateLength) Like DateFormat(intFrmtCtr) Then 
GetDate = DateValue(Mid(strInput, intPosition, intDateLength)) 
Exit Function 
End If 

Next intPosition 

End If 

Next intFrmtCtr 

End Function

2nd macro
1) select B2:E2
2) =getdate2(A2)
3) confirm with Ctrl + Shift + Enter (array formula)

Code:
Function getdate2(txt As String) 
Dim m As Object, myTxt As String 
With CreateObject("VBScript.RegExp") 
     .Pattern = "(\d{1,2}(/|-)){2}(\d{2}|\d{4})" 
     .Global = True 
     For Each m In .execute(txt) 
          myTxt = myTxt & "," & m.Value 
     Next 
End With 
getdate2 = Split(Mid$(myTxt,2),",") 
End Function

With this second macro It works great when there are multiple dates:
ie.
Let's say A2 = Reopened for additional information. //PRP 10-27-06 Reopened for adjudication.//pv 12-15-06
When I highlight B2:E2 and then type in the formula =getdate2(A2) and press CTRL+SHIFT+ENTER (array formula). It fills in B2 & C2 with 10-27-06 and 12-15-06 respectively; unfortunately D2 & E2 get a #N/A.
I would like to figure out how to resolve that with just a blank

If let's say A2 =
Reopened for additional information. //PRP 10-27-06 (which obviously only has one date), and I perform this same function Highlight B2:E2 & then type in =getdate2(A2), press CTRL+SHIFT+ENTER(array formula). It fills in B2:E2 all with 10-27-06.

Can one of you EXPERTS please take a look at both of these Macros and see if you can help me. I really need to get the dates automatically extracted from this text string field, else it's a very long manual process of manually typing out the dates. I know it can be done, because I'm so close with these two macros, I can just feel it.
 
Upvote 0
Hi aka_krakur

I see 3 solutions for your question

The first 2 use your getdate2()

1 - This one involves some extra work. Change the return of the function to an array with a fixed number of positions (ex. 4). The first positions will have the dates in the string and the last ones are empty strings. This way if you always select 4 cells you'll get no error. I don't like this solution (but it would work)

2 - Easy. Use your getdate2 and trap the errors. instead of entering the formula in the worksheet as a formula that returns an array (select several cells and CTRL+SHIFT+ENTER), pick up the elements one by one. In case of error write the empty string.

By the way, change the end of the pattern of the regex from (\d{2}|\d{4}) to (\d{4}|\d{2}) or you'll never get 4 digit years.

In B2:
Code:
=IF(ISERROR(INDEX(Getdate2($A2),COLUMNS($B:B))),"",0+INDEX(Getdate2($A2),COLUMNS($B:B)))
Copy down and accross

3 - Instead of an UDF use a macro to do the job automatically.
This is a suggestion:
Code:
Sub ParseDates()
Dim rRng As Range, rCell As Range
Dim oMatches As Object, i As Integer

Set rRng = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

With CreateObject("VBScript.RegExp")
    .Pattern = "(\d{1,2}[\/\-]){2}(\d{4}|\d{2})"
    .Global = True
    For Each rCell In rRng
        Set oMatches = .Execute(rCell)
        If oMatches.Count <> 0 Then
            For i = 0 To oMatches.Count - 1
                rCell.Offset(, i + 1) = oMatches(i).Value
            Next i
        End If
    Next rCell
End With
End Sub

I prefer the solutions 2 and 3.

Hope this helps
PGC

P. S. I'm leaving. I'll check this thread again tomorrow.
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,357
Members
449,155
Latest member
ravioli44

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