I have this VBA module in my Excel spreadsheet that attempts to clean up the date data, which contains various issues with text being combined with date information. Here is my main load function:
Public lstrow As Long, strDate As Variant, stredate As Variant
Sub importbuild()
lstrow = Worksheets("Data").Range("G" & Rows.Count).End(xlUp).Row
Function DateOnlyLoad(col As String, col2 As String, colcode As String)
Dim i As Long, j As Long, k As Long
j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
strDate = spacedate(Worksheets("Data").Range(col & i).Value)
stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)
If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1,
UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
GoTo EmptyRange
Else
Worksheets("CI").Range("A" & j & ":C" & j).Value =
Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("CI").Range("D" & j).Value = colcode
Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
'Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
Worksheets("CI").Range("F" & j).Value = strDate
If col2 <> "NA" Then
If IsEmpty(stredate) = False Then
Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
End If
End If
j = j + 1
End If
EmptyRange:
Next i
End Function
datecleanup function:
Function datecleanup(inputdate As Variant) As Variant
If Len(inputdate) = 0 Then
inputdate = "01/01/1901"
Else
If Len(inputdate) = 4 Then
inputdate = "01/01/" & inputdate
Else
If InStr(1, inputdate, ".") Then
inputdate = Replace(inputdate, ".", "/")
End If
End If
End If
datecleanup = Split(inputdate, Chr(32))(0)
Sample Output:
Column A Column B Column C Column D Column E Column F
125156 Wills, C 11/8/1960 MMR1 MUMPS MUMPS TITER 02/26/2008 POSITIVE
291264 Balti, L 09/10/1981 MMR1 (blank) Measles - 11/10/71 Rubella
943729 Barnes, B 10/10/1965 MMR1 MUMPS MUMPS TITER 10/08/2008 POSITIVE
The Split separates the date from the subsequent text and this works fine, however if there is text that occurs before the date then the output contains the first part of the text. I would like to get only the date (if it exists) from the string and display that, regardless of where it falls in the string. Below are sample results: Column E is the output from the Split logic, Column F is the entire string that is being evaluated from the other worksheet.
Desired Output from above examples: (Column E has correct dates extracted)
Column A Column B Column C Column D Column E Column F
125156 Wills, C 11/8/1960 MMR1 02/26/2008 MUMPS TITER 02/26/2008 POSITIVE
291264 Balti, L 09/10/1981 MMR1 11/10/71 Measles - 11/10/71 Rubella
943729 Barnes, B 10/10/1965 MMR1 10/08/2008 MUMPS TITER 10/08/2008 POSITIVE
What else can I add into my datecleanup function to further refine this? Thanks in advance!