User Form Date Validation

Chris Williamson

Board Regular
Joined
Oct 16, 2010
Messages
83
Hi Guys,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I’ve got a user form with a few text boxes on it.<o:p></o:p>
TextBox4 is for a date to be entered in it.<o:p></o:p>
<o:p></o:p>
To assist the user to enter the date in the correct format I’ve got in the ‘TextBox4 properties Value field’ the text “dd mmm yyyy”. <o:p></o:p>
But some people are still inputting dates in the wrong format.<o:p></o:p>
<o:p></o:p>
Q) What would the vb macro code be if I wanted the user form to convert the date that is entered into the correct format when they tab to the next text box on the user form?<o:p></o:p>
<o:p></o:p>
The ideal format that I want is: dd mmm yyyy, <o:p></o:p>
So today’s date is: <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:date Year="2012" Day="17" Month="5">18 May 2012</st1:date><o:p></o:p>
<o:p></o:p>
The incorrect formats that some people input are:<o:p></o:p>
<st1:date Year="2012" Day="17" Month="5">18/05/2012</st1:date><o:p></o:p>
<st1:date Year="2012" Day="17" Month="5">18/05/12</st1:date><o:p></o:p>
<st1:date Year="2012" Day="17" Month="5">18/5/12</st1:date><o:p></o:p>
18 may 12<o:p></o:p>
18 May 12<o:p></o:p>
<o:p></o:p>
Thanks for your help.<o:p></o:p>
<o:p></o:p>
Chris<o:p></o:p>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try This,

To Check date is a Valid date

Code:
If IsDate(Trim(Textbox4.Text)) = False Then
MsgBox "Please enter the correct date"
End If

'To convert date into dd mmm yyyy format

Code:
Textbox4.Text = Format(Textbox4.Text, "dd mmmm yyyy")
 
Upvote 0
i have a similar problem with my User form. My current code is as below and anytime the user enters a date in a DateRaised_TX textbox, hte date is returned in the US format of mm/dd. Any ideas why?
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Risk_Register")
Dim n As Long
n = sh.Range("I" & Application.Rows.Count).End(xlUp).Row

sh.Range("I" & n + 1).Value = Me.EpicName_CB.Value
sh.Range("j" & n + 1).Value = Me.DateRaised_TX.Value
sh.Range("l" & n + 1).Value = Me.RaisedBy_TX.Value
sh.Range("m" & n + 1).Value = Me.RiskType_CB.Value
sh.Range("n" & n + 1).Value = Me.RiskName_TX.Value
sh.Range("o" & n + 1).Value = Me.RiskDescription_TX.Value
sh.Range("p" & n + 1).Value = Me.ImpactDescription_TX.Value
sh.Range("q" & n + 1).Value = Me.AreaOfImpact_CB.Value
sh.Range("r" & n + 1).Value = Me.RiskValue_TX.Value
sh.Range("t" & n + 1).Value = Me.RiskOWner_TX.Value
sh.Range("u" & n + 1).Value = Me.Impact_CB.Value
sh.Range("v" & n + 1).Value = Me.Probablility_CB.Value
sh.Range("x" & n + 1).Value = Me.MitigationAction_TX.Value
sh.Range("y" & n + 1).Value = Me.PostMitigationValue_TX.Value
sh.Range("aa" & n + 1).Value = Me.ResolveDate_TX.Value


If Me.OptionButton1.Value = True Then
sh.Range("k" & n + 1).Value = "Open"
End If
If Me.OptionButton2.Value = True Then
sh.Range("k" & n + 1).Value = "Closed"
End If
If Me.OptionButton3.Value = True Then
sh.Range("k" & n + 1).Value = "For Escalation"
End If
 
Upvote 0
Use CDate to convert the text to a real date value using your regional settings:

Code:
sh.Range("j" & n + 1).Value = CDate(Me.DateRaised_TX.Value)
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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