Enter date dd/mm/yyyy in userform textbox

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I have a userform textbox where a date needs to be entered in the format:

dd/mm/yyyy

I would like the phrase "dd/mm/yyyy" to appear in light grey colour when the textbox is empty and when the user clicks on the textbox and writes a date, the font colour should change to black.

Also, if there is a way to validate if the date is entered in the correct format it would be much appreciated.

thanking you in advance
Andy
 
Hello,

This is one I can answer. Firstly select the userform in question and select date field. Press F4 to show the properties for that field then scroll down to the field named text and input dd/mm/yyyy and this will mask the entry into the userform.

Hope this helps. :biggrin:
 
Upvote 0
Hi, Try this:-
The "CommandButton" Code should be on the "Userform" At the start of your code, that does something with the Date entry in the "TextBox"

To clear the Text Box, and Enter a Date, You Have to Double click the TextBox . You could Add a Label to the "Userform" with this instruction.

The "UserForm_Initialize()" code Sets the Font to Grey And places the Date Text "dd/mm//yy" in the TextBox.
Code:
Private Sub CommandButton1_Click()
If Not IsDate(UserForm8.TextBox1.Text) Then
MsgBox """The Data entered is not a Date""" _
    & Chr(10) & "          ""Please Try again"""
Exit Sub
End If
End Sub
Code:
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With UserForm8.TextBox1
    .Text = ""
    .ForeColor = RGB(0, 0, 51)
End With
End Sub
Code:
Private Sub UserForm_Initialize()
With UserForm8.TextBox1
    .Text = "dd/mm/yy"
    .ForeColor = RGB(204, 204, 204)
    .Font.Size = 14
End With
End Sub
Regards Mick
 
Upvote 1
Why not use the Enter event instead of DoubleClick?
Code:
Private Sub CommandButton1_Click()
    If Not IsDate(UserForm8.TextBox1.Text) Then
        MsgBox "The Data entered is not a Date" & Chr(10) & "Please Try again"
        Exit Sub
    End If
End Sub
Private Sub TextBox1_Enter()
    With Me.TextBox1
        .Text = ""
        .ForeColor = RGB(0, 0, 51)
    End With
    
End Sub
Private Sub UserForm_Initialize()
    
    With Me.TextBox1
        .Text = "dd/mm/yy"
        .ForeColor = RGB(204, 204, 204)
    End With
    
End Sub
Note this will only work if there are mutiple controls on the form and the textbox in question is not the first in the tab order.
 
Upvote 0
This is very annoying.

I have a procedure which writes the date entered in the textbox into a cell.

However instead of writing the date as expected "dd/mm/yyyy" it writes it as "mm/dd/yyyy"

I have even checked the format settings of the cell and they are UK date "dd/mm/yyyy"...

I don't know what's going wrong here..

the code is simply

Code:
Sheet1.range("A1").value = userform.textbox1.value
 
Upvote 0
Try this.
Code:
Sheet1.Range("A1").value = DateValue(Me.textbox1.value)
 
Upvote 0

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