Validate UserForm textbox to accept only the correct date format

SSF1590

Board Regular
Joined
Oct 20, 2019
Messages
73
I am looking for a code that allows my textbox5 in my UserForm to accept or validate only the correct date format mm/dd/yyyy as input. Is it possible to do this? I’ve been doing some research but I can find something I can make work. Can I please have your help?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
There is a reason that many (most?) forms have three controls to input Date, Month and Year seperatly.

Its possible to test an entry to see if it is a date.

VBA Code:
Private TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean)
    With TextBox1
        If IsDate(.Text) Then
            .Text = Format(DateValue(.Text), "mm/dd/yyyy")
        Else
            MsgBox "Not a date"
            Cancel = True
        End If
    End With
End Sub
 
Upvote 0
There is a reason that many (most?) forms have three controls to input Date, Month and Year seperatly.

Its possible to test an entry to see if it is a date.

VBA Code:
Private TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean)
    With TextBox1
        If IsDate(.Text) Then
            .Text = Format(DateValue(.Text), "mm/dd/yyyy")
        Else
            MsgBox "Not a date"
            Cancel = True
        End If
    End With
End Sub
Hello, thank you for your contribution on this. Is the private missing sub in the above code?
 
Upvote 0
There is a reason that many (most?) forms have three controls to input Date, Month and Year seperatly.

Its possible to test an entry to see if it is a date.

VBA Code:
Private TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean)
    With TextBox1
        If IsDate(.Text) Then
            .Text = Format(DateValue(.Text), "mm/dd/yyyy")
        Else
            MsgBox "Not a date"
            Cancel = True
        End If
    End With
End Sub
I have added sub to the line Private Sub TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean) but it gives me syntax error in the entire line.
 
Upvote 0
It needs a space between Cancel and As
It is working perfectly! Thank you very much for your help on this. Would it be possible to highlight the wrong date entered (textbox background) with a different color so it is obvious for the user where the error is?
 
Upvote 0
VBA Code:
Private TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean)
    With TextBox1
        If IsDate(.Text) Then
            .Text = Format(DateValue(.Text), "mm/dd/yyyy")
            .BackColor = vbWhite
        Else
            MsgBox "Not a date"
            .BackColor = vbYellow
            Cancel = True
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,108
Messages
6,163,968
Members
451,867
Latest member
csktwyr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top