Setting Date Format for a Text Box in a User Form

garbfink

Board Regular
Joined
Dec 14, 2009
Messages
56
Hi All,

I've been struggling with this for a while now and can't believe how hard it is!. I've searched on this site and on others to get some clarification but to no avail.

It's pretty simple really. I have a user form which contains a tex box for a user to input the date I want the format to be dd/mm/yyyy but can't find out how to set the format of the text box to this.

Please can someone give me a bit of guidence or link me to a good rescource if missed it in the search.

Thanks in advance.

G
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I would either add the calendar control, or use a spinner for year, month and day. Textbox input isn't worth the bother.
 
Upvote 0
Code:
        If Not IsDate(TextBox1) Then
            MsgBox "Please input a valid date"
            Exit Sub
        Else
            YourVariable = Format(TextBox1, "dd/mm/yyyy")
        End If

Edit: xld's suggestion is much better :)
 
Last edited:
Upvote 0
I would either add the calendar control, or use a spinner for year, month and day. Textbox input isn't worth the bother.

Code:
        If Not IsDate(TextBox1) Then
            MsgBox "Please input a valid date"
            Exit Sub
        Else
            YourVariable = Format(TextBox1, "dd/mmm/yyyy")
        End If

xld's suggestion is much better :)

Yeah I thought I had it nailed with the calendar control but when I distributed it to some staff to use half of them got the below message whenenver the user form was openend.

"Could not load an object because it is not available on this system"

I'm going to try and work on the macro above.

Thanks
 
Upvote 0
Yeah I thought I had it nailed with the calendar control but when I distributed it to some staff to use half of them got the below message whenenver the user form was openend.

"Could not load an object because it is not available on this system"

I'm going to try and work on the macro above.

Thanks

You could sitribute the calendar control as well, or just use pinners as I suggested.
 
Upvote 0
The problem with the other approach is that it doesn't ensure a valid date is input, you should really check it as they type, and that is a real faff.
 
Upvote 0
Yup. I've just tested the code I posted and if it's before the 12th of the month; it flips the day and the month in the output. Lots of faff required to get it working properly.
 
Upvote 0
Yup. I've just tested the code I posted and if it's before the 12th of the month; it flips the day and the month in the output. Lots of faff required to get it working properly.

That should easily be remedied with

Code:
        If Not IsDate(TextBox1) Then
            MsgBox "Please input a valid date"
            Exit Sub
        Else
            YourVariable = Format(CDate(TextBox1.Text), "dd/mm/yyyy")
        End If

but it still doesn't validate as input
 
Upvote 0
You could sitribute the calendar control as well, or just use pinners as I suggested.

how would i distibute this control?

I've been messing around with the vba and am having the day / month juggling issue even with xld's revised code.

I'm either going to have it as just a straight text box, or implement the spinners.

I can't believe that setiing a format on a text box is so annoying!

Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,044
Members
448,543
Latest member
MartinLarkin

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