I have a function to reformat dates.
Public Function FixDate(tRevisionDate)
Dim Y
Dim M
Dim D
Dim arrRevisionDate
Debug.Print "RevisionDate In Is: " & tRevisionDate
If Len(tRevisionDate) < 10 Then
tRevisionDate = Replace(tRevisionDate, "/", "-")
tRevisionDate = Replace(tRevisionDate, ".", "-")
arrRevisionDate = Split(tRevisionDate, "-")
M = arrRevisionDate(0)
D = arrRevisionDate(1)
Y = arrRevisionDate(2)
If Len(M) < 2 Then
M = "0" & M
End If
If Len(D) < 2 Then
D = "0" & D
End If
If Len(Y) < 4 Then
Y = "20" & Y
End If
tRevisionDate = Y & "-" & M & "-" & D
End If
Debug.Print "RevisionDate Out Is: " & tRevisionDate
End Function
I'm calling it like this:
RevisionDate = FixDate(tRevisionDate)
If I do a debug.print
Debug.Print "RevisionDate=" & RevisionDate
I get NOTHING. The debug.print commands from within the funtion return the correct values.
What AM I Doing WRONG?
Thanks In Advance!
Since the FixDate() is declared as Function, you need return some value in the last line of executing code:
Public Fucntion FixDate(....)
...
Debug.Print "RevisionDate Out Is: " & tRevisionDate
''Add this line of code
FixDate=tRevisionDate
End Function
But your code seems overly complicated. If the input (tRevisionDate) is a value Date/Time string, you can
Public Function FixDate(tRevisionDate As String)
''Convert the string into a Date type
''You may want to trap possible error (On Error ,,,) here
''in case the input is invalid. Tha is
''if the input cannot be converted into a Date type,
''you may want to return Nothing or blank string
Dim d As Date
d=CDate(tRevisionDate
''reformat it in a desired date string
FixDate=Format(d,"yyyy-MM-dd")
End Function
Norman Yuan