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?
 
VBA Code:
Private TextBox1_BeforeUpdate(ByVal CancelAs MSForms.ReturnBoolean) and when I changed 
    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
Hello again. I just have a question. When I enter date 15/01/19 into the textbox where I inserted the code, it corrects it to 01/19/15. Would it be possible to make the code force to take 19 as the year to make it 01/15/2019? Also even when the code calls for / to separate month, day and year, the textbox corrects it to -. So, I insert 11/26/2019 it changes it to 11-26-2019. I did not realize this before because when I tried in one computer it work, changing to / (slashes) but when I opened in another computer it changed it to - (dashes).
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
"01/18/15" is not in the date format specified in the code. The code specified that the last numeral should have four digits. And the code also specifies that the separator be / not -. Are you sure that you are working on the right text box?
 
Upvote 0
That’s what I was thinking regarding the “01/18/15”. Reafarsing the separator, I know the code calls for / but if I used a different computer with different office then it gives - as separator. I’m not sure why
 
Upvote 0
Is it going into the text box. Do you have the textbox linked to a cell? Is it the textbox that you think it is or might it be a different textbox (with different code)?
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,499
Members
453,047
Latest member
charlie_odd

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