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
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
I would either add the calendar control, or use a spinner for year, month and day. Textbox input isn't worth the bother.
 

id107

Well-known Member
Joined
Apr 16, 2008
Messages
578
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:

garbfink

Board Regular
Joined
Dec 14, 2009
Messages
56
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
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378

ADVERTISEMENT

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.
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
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.
 

id107

Well-known Member
Joined
Apr 16, 2008
Messages
578

ADVERTISEMENT

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.
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
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
 

garbfink

Board Regular
Joined
Dec 14, 2009
Messages
56
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,820
Messages
5,598,301
Members
414,224
Latest member
Crazy_FC

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