The keypress will work. I think I have it in a few of my older workbooks. Xld gives you an example even. But I am still partial to the comboboxes.![]()
The keypress will work. I think I have it in a few of my older workbooks. Xld gives you an example even. But I am still partial to the comboboxes.![]()
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
XLD,
Thanks for your input, huge help.
All,
I am attaching my version of a TextBox that will capture a date that a user inputs.
ANY suggestions by anyone would greatly be appreciated.
Oh ya, here is the code incase you dont want to download the file...
Option Explicit Private Sub CommandButton1_Click() If IsDate(TextBox1.Value) = False Or Left(TextBox1.Value, 2) > 12 Or _ Mid(TextBox1.Value, 4, 2) > 31 Then MsgBox ("This is not a date, Please try again") _ Else MsgBox ("This is a date, Good for you") End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim Temp As String Select Case KeyAscii Case Asc("0") To Asc("9") Case Else: KeyAscii = 0 End Select If Len(TextBox1) = 2 Or Len(TextBox1) = 5 Then _ TextBox1.Value = TextBox1.Value & "/" End if End Sub Private Sub UserForm_Initialize() TextBox1.Value = Format(Date, "MM/DD/YY") CommandButton1.SetFocus End Sub
Last edited by Aussiebear; 03-23-2025 at 08:58 AM.
This is overkill as the IsDate function will not allow an invalid number of days or months, so all you need is
In addition, your way only works for US dates, as we continentals use a dd/mm/yy format, where the first two digits can be greater than 12. Mine works for any.Private Sub CommandButton1_Click() If Not IsDate(TextBox1.Value) Then MsgBox ("This is not a date, Please try again") Else MsgBox ("This is a date, Good for you") End If End Sub
.
Last edited by Aussiebear; 03-23-2025 at 08:59 AM.
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
XLD,
Thanks for the heads-up... However, if I wanted to ensure that a US date only was entered correctly, would my way surfice?
The rare times I get to create files that capture date(s) would only be that of US Dates. I will, nevertheless, be mindful of US dates and Continental dates.
Thanks for all your input/help
Your way works for US only dates if it will only be used in the US, but over here it fails because it will not let me input a day of 13, and if I revert the format, the IsDate fails.Originally Posted by Philcjr
My suggestion works for US dates in the US, continental dates in the continent.
.
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber