I'm looking for a way to automatically format the date in a VBA text box to a MM/DD/YYYY format, and I want it to format as the user is typing it in. For instance, once the user types in the second number, the program will automatically type in a "/". Now, I got this working (as well as the second dash) with the following code:
Private Sub txtBoxBDayHim_Change()
If txtBoxBDayHim.TextLength = 2 or txtBoxBDayHim.TextLength = 5 then
txtBoxBDayHim.Text = txtBoxBDayHim.Text + "/"
End Sub
Now, this works great when typing. However, when trying to delete, it still enters in the dashes, so its impossible for the user to delete past one of the dashes (deleting a dash results in a length of 2 or 5, and the sub is then run again, adding in another dash). Any suggestions on a better way to do this?
For a quick solution, I usually do like this.
This approach will allow the user to enter date in any format they like in the textbox, and finally format in mm/dd/yyyy format when he is done editing. So it is quite flexible:
However, I think what Sid developed is a much better approach - a full fledged date picker control.
This is the same concept as Siddharth Rout's answer. But I wanted a date picker which could be fully customized so that the look and feel could be tailored to whatever project it's being used in.
You can click this link to download the custom date picker I came up with. Below are some screenshots of the form in action.
To use the date picker, simply import the CalendarForm.frm file into your VBA project. Each of the calendars above can be obtained with one single function call. The result just depends on the arguments you use (all of which are optional), so you can customize it as much or as little as you want.
For example, the most basic calendar on the left can be obtained by the following line of code:
That's all there is to it. From there, you just include whichever arguments you want to get the calendar you want. The function call below will generate the green calendar on the right:
Here is a small taste of some of the features it includes. All options are fully documented in the userform module itself:
I too, one way or another stumbled on the same dilemma, why the heck Excel VBA doesn't have a
Date Picker
. Thanks to Sid, who made an awesome job to create something for all of us.Nonetheless, I came to a point where I need to create my own. And I am posting it here since a lot of people I'm sure lands on this post and benefit from it.
What I did was very simple as what Sid does except that I do not use a temporary worksheet. I thought the calculations are very simple and straight forward so there's no need to dump it somewhere else. Here's the final output of the calendar:
How to set it up:
Label
controls and name it sequentially and arranged left to right, top to bottom (This labels contains greyed25
up to greyed5
above). Change the name of theLabel
controls to Label_01,Label_02 and so on. Set all 42 labelsTag
property todts
.Label
controls for the header (this will contain Su,Mo,Tu...)Label
control, one for the horizontal line (height set to 1) and one for the Month and Year display. Name theLabel
used for displaying month and year Label_MthYrImage
controls, one to contain the left icon to scroll previous months and one to scroll next month (I prefer simple left and right arrow head icon). Name itImage_Left
andImage_Right
The layout should be more or less like this (I leave the creativity to anyone who'll use this).
Declaration:
We need one variable declared at the very top to hold the current month selected.
Private Procedure and Functions:
Image Events:
I added this to make it look like the user is clicking the label and should be done on the
Image_Right
control too.Label Events:
All of this should be done for all 42 labels (
Label_01
toLable_42
)Tip: Build the first 10 and just use find and replace for the remaining.
This is for hovering over dates and clicking effect.
UserForm Events:
Again, just for the hovering over dates effect.
And that's it. This is raw and you can add your own twist to it.
I've been using this for awhile and I have no issues (performance and functionality wise).
No
Error Handling
yet but can be easily managed I guess.Actually, without the effects, the code is too short.
You can manage where your dates go in the
select_label
procedure. HTH.