Extract date from text range

R

Richard Carter

A text string in my spreadsheet includes dates, which need to be
extracted in order to perform calculations. I have found no way of
dealing with all the problems.


This is what I need to achieve:
Cell A1 says "Purchase order for £5,000 subcontractor renewal, 1st
April 2003 to 31/3/04. Cost as per quote @ 1st February 03."

Cell A2 must return 01-Apr-03
Cell A3 must return 31-Mar-04

The supplier quote date is irrelevant in this case.


I have tried removing all text characters. That leaves me with
garbage: "£5,000, 1 31/3/04. @ 1 03." I've also tried separating
into columns using space as a delimiter. This also fails to deal
with 1st April 2003 as a single date.

Any ideas!?

Regards etc,
Richard Carter

========================
Richard Carter
 
D

Don Guillett

have you tried using the find function to find the , since it is the only
one and teh to since is is the only one and applying the find to the mid
function?
 
D

Dave Peterson

I think I'd try to parse it out into its pieces.

first thing I'd do is get rid of the st, nd, rd, th stuff (1st, 2nd, 3rd, 4th,
....).

then look until I found the 2000 surrounded by important stuff.
(I looked for " 20## to ")
then go backwards to pick out the first date and go forward to pick out the
second.
(If your dates go back to 1999 (and before), this'll fail.)

If either failed, then I tossed out a #ref error.

This is the code I used:

Option Explicit
Function twoDates(myCell As Range) As Variant

Dim iCtr As Long
Dim SpacePosBeforeYear As Long
Dim startingSpacePos As Long
Dim endingSpacePos As Long
Dim FoundSpace As Long
Dim myDates(0 To 1) As Date
Dim mySplit As Variant
Dim myStr As String
Dim testDate As Variant

If Application.Caller.Cells.Count <> 2 Then
twoDates = CVErr(xlErrRef)
Exit Function
End If

Set myCell = myCell.Cells(1, 1)

myStr = myCell.Value
myStr = Application.Substitute(myStr, "nd ", " ")
myStr = Application.Substitute(myStr, "st ", " ")
myStr = Application.Substitute(myStr, "rd ", " ")
myStr = Application.Substitute(myStr, "th ", " ")

SpacePosBeforeYear = 0
For iCtr = 1 To Len(myStr)
If Mid(myStr, iCtr, 9) Like " 20## to " Then
SpacePosBeforeYear = iCtr
Exit For
End If
Next iCtr

If SpacePosBeforeYear = 0 Then
twoDates = CVErr(xlErrRef)
Exit Function
End If

FoundSpace = 0
For iCtr = SpacePosBeforeYear - 1 To 1 Step -1
If Mid(myStr, iCtr, 1) = " " Then
FoundSpace = FoundSpace + 1
If FoundSpace = 2 Then
startingSpacePos = iCtr
Exit For
End If
End If
Next iCtr

testDate = Mid(myStr, startingSpacePos + 1, _
(SpacePosBeforeYear + 5) - startingSpacePos - 1)
If IsDate(testDate) Then
myDates(0) = CDate(testDate)
Else
twoDates = CVErr(xlErrRef)
Exit Function
End If

endingSpacePos = 0
For iCtr = SpacePosBeforeYear + 9 To Len(myStr)
If Mid(myStr, iCtr, 1) = " " Then
endingSpacePos = iCtr
Exit For
End If
Next iCtr

If endingSpacePos = 0 Then
twoDates = CVErr(xlErrRef)
Exit Function
End If

testDate = Mid(myStr, SpacePosBeforeYear + 9, _
(endingSpacePos - 2) - (SpacePosBeforeYear + 9) + 1)
mySplit = Split97(testDate, "/")

'check for 3 parts
If UBound(mySplit) - LBound(mySplit) + 1 = 3 Then
myDates(1) = DateSerial(mySplit(UBound(mySplit)) + 2000, _
mySplit(LBound(mySplit) + 1), _
mySplit(LBound(mySplit)))
Else
twoDates = CVErr(xlErrRef)
Exit Function
End If

If Application.Caller.Columns.Count = 2 Then
twoDates = myDates
Else
twoDates = Application.Transpose(myDates)
End If

End Function

'from Tom Ogilvy
Function Split97(sStr As Variant, sdelim As String) As Variant
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function

===============
If you're using xl2k or higher, you can replace:
application.substitute with Replace
and
split97 with split
(and delete that useful function from Tom Ogilvy)

So if your text were in A1, you could highlight A2:A3 (with A2 the activecell)
and type:

=twodates(a1)
and hit ctrl-shift-enter

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

=======

Short course:
Hit alt-f11 to get to the VBE (where macros/UDF's live)
hit ctrl-R to view the project explorer
Find your workbook.
should look like: VBAProject (yourfilename.xls)

right click on the project name
Insert, then Module
You should see the code window pop up on the right hand side

Paste the code in there.

Now go back to excel.
Type in that formula in A2:A3 and hit ctrl-shift-enter.
 
J

JohnI

Richard,

Got the second date in A3 as -
=DATEVALUE(MID(A1,FIND(" to ",$A1)+4,FIND(". ",$A1)-FIND(" to ",$A1)-4))

The first date is tedious because of the "1st" part.
Here's a start -
=(MID(A1,FIND(", ",$A1)+2,FIND(" to ",$A1)-FIND(", ",$A1)-2))

regards,

JohnI
 
R

Richard Carter

Thanks for these - all seem to work in their own way but the
problem is that I have about 4,500 cells with similar paragraphs
of text. The dates are, of course, in different formats and there
is no common denominator.

Any more suggestions?

Regards etc,
Richard Carter

========================
richard at rjcarter dot net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top