User Form Date Validation

Chris Williamson

Board Regular
Joined
Oct 16, 2010
Messages
82
Hi Guys,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><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:eek:ffice: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>
 

Some videos you may like

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

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,194
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")
 

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,711
Why not use a microsoft date and time picker control? There's no typing then :)
 

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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)
 

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows
Fab! I really appreciate it!

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)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,487
Messages
5,625,054
Members
416,067
Latest member
newb08

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
Top