Thread: Code Needed
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.newusers
jamescox[_52_] jamescox[_52_] is offline
external usenet poster
 
Posts: 1
Default Code Needed


In your userform's code, outside of any other subroutines you currently
have, you would need to add a subroutine like the following



Private Sub TxtDate_BeforeUpdate(ByVal Cancel As
MSForms.ReturnBoolean)

Me.TxtDate.Value = Format(CDate(Trim(Me.TxtDate.Value)),
"dd/mm/yyyy")

End Sub



This event code will fire after the user enters any text in the TxtDate
textbox and then either presses Enter or clicks on another text box.

However, while testing the above then re-reading your original post,
there is an additional problem. You want to enter the date as dd/mm
(where either of those could be a single digit). *BUT* Excel wants the
month first, not the date. The CDate function returns "1/9" as the 9th
of January, but "13/9" is rendered as the 13th of September - because
Excel is assuming you really meant 9/13. The code below will allow you
to enter dd/mm, but if you or your user enters mm/dd instead, it will
screw up the date. (Actually, I played for a while and didn't find a
date format for cells in Excel that would interpret 23/9 as a date at
all - though that's not relevant to the code below.)

At any rate, while I would suggest that you use some other format for
date entry (never can tell what those crazy end-users will do...), the
following will support *and require* entry in the dd/mm format - and
will also handle some simple user screwups on data entry.


Private Sub TextBox1_BeforeUpdate(ByVal Cancel As
MSForms.ReturnBoolean)

Dim sEntry As String
Dim iLoc As Integer

sEntry = Trim(Me.TextBox1.Value)
iLoc = InStr(sEntry, "/")
If iLoc 0 Then
sEntry = Right$(sEntry, Len(sEntry) - iLoc) & "/" &
Left$(sEntry, iLoc - 1)
On Error Resume Next
Me.TextBox1.Value = Format(CDate(sEntry), "dd/mm/yyyy")
If Err < 0 Then
GoTo Had_Problem
End If
Exit Sub
End If

Had_Problem:
MsgBox "Could not interpret your entry as a date in the format of
d/m." & vbLf & "Please try again..."
Cancel = True

End Sub


You'll have to change the text box name in the code above - my test
userform didn't have the name you used...


--
jamescox
------------------------------------------------------------------------
jamescox's Profile: http://www.thecodecage.com/forumz/member.php?userid=449
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=114956