View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
JacyErdelt JacyErdelt is offline
external usenet poster
 
Posts: 10
Default Another Question about Date Formatting

I apologize if this is redundant, but I am having a small problem. The
purpose of the following code is to allow the user to enter a date into a
textbox without having to enter slashes or hyphens (040109 = 04/01/09). It
works for the most part, but instead of coming out as 04/01/09, it comes out
as 01/04/09. Every time it swtiches the day and month. Any suggestions as to
why it might be doing this, and what I can do to fix it. Here is what I have;

Private Sub txtDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim nDay As Long, nMonth As Long, nYear As Long
Dim d As Date

If IsDate(txtDate.Value) = True Then
txtDate.Value = Format(txtDate.Value, "mm/dd/yy")
Else:
nDay = CLng(Left(txtDate.Text, 2))
nMonth = CLng(Mid(txtDate.Text, 3, 2))
nYear = CLng(Right(txtDate.Text, Len(txtDate.Text) -4 ))
d = DateSerial(nYear, nMonth, nDay)

txtDate.Value = d
txtDate.Value = Format(txtDate.Value, "mm/dd/yy")
End If

End Sub