ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   textbox - format for dates (mm/dd/yyy) (https://www.excelbanter.com/excel-programming/286032-textbox-format-dates-mm-dd-yyy.html)

barrfly[_2_]

textbox - format for dates (mm/dd/yyy)
 
I have a text box in a userform. The box will be used to manually ente
a date, so I would like the "/" to appear between the month & day
year as the date is entered as well as not allow invalid date entries.


B

--
Message posted from http://www.ExcelForum.com


Jake Marx[_3_]

textbox - format for dates (mm/dd/yyy)
 
Hi BA,

There are several ways to do this. The best may be to use a specialized
control instead of a TextBox...perhaps the Date/Time Picker. But you'll
have to check licensing requirements and availability on client machines
before using one.

If you want to use a textbox, you could put some code behind it like this:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
If IsDate(.Text) Then
.Text = Format$(.Text, "Short Date")
Else
.SelStart = 0
.SelLength = Len(.Text)
MsgBox "Invalid date entered.", vbExclamation
Cancel = True
End If
End With
End Sub


If you want the /'s to show up, then it would be somewhat trickier. Your
best bet in that case may be to use 3 separate textboxes separated by labels
with captions of "/". Then you could validate the concatenation of the
textboxes assuming all three are filled in. If all three aren't filled in,
you can raise an error when they attempt to proceed.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


barrfly wrote:
I have a text box in a userform. The box will be used to manually
enter a date, so I would like the "/" to appear between the month &
day & year as the date is entered as well as not allow invalid date
entries.


BA


---
Message posted from http://www.ExcelForum.com/




All times are GMT +1. The time now is 11:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com