Date format in userform textbox

hkbhansali

Board Regular
Joined
Oct 26, 2013
Messages
51
I'm looking for a way to automatically format the date in a VBA text box via userform to a dd/mmm/yyyy format,
and I want it to format as the user is typing it in.
and also want the same text box to always pick the date from column "D" in dd/mmm/yyyy format but date should be editable.
(in my worksheet column "D" has same dd/mmm/yyyy format)
Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You give little information so I'll have to guess

assuming your textbox is called Textbox1 and your date is in cell D1, paste this into the code window of the userform:

(nb: you can't format the textbox as you type as it will always assume the current year. format it on lost focus)

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If IsDate(TextBox1.Text) Then TextBox1.Text = Format(TextBox1.Text, "dd/mmm/yyyy")
End Sub


Private Sub UserForm_Initialize()
    'set the date from cell D1
    If IsDate(Sheet1.Range("D1").Value) Then TextBox1.Text = Format(Sheet1.Range("D1").Value, "dd/mmm/yyyy")
End Sub
 
Upvote 0
Hi, gallen
Thanks for reply.
my textbox is "txtdate" and date store in column "D" -("D2:D500).
your code can't display date in "dd/mmm/yyyy" format.
 
Last edited:
Upvote 0
It was tested and worked fine on a userform textbox for me.

How does it determine which cell in column D to get the date from?
 
Upvote 0
I don't understand what you mean by "fomat as the user is typing"


If I want to enter June 3, 2016, what should happen when I press the "J"?

If you want to change the format to yours after the user is done entering, gallen's code is the way to go.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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