Formatting MM/DD/YYYY dates in textbox in VBA

前端 未结 9 1746
故里飘歌
故里飘歌 2020-11-22 10:47

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

9条回答
  •  悲&欢浪女
    2020-11-22 11:26

    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.

    Three example calendars

    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:

    MyDateVariable = CalendarForm.GetDate
    

    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:

    MyDateVariable = CalendarForm.GetDate( _
        SelectedDate:=Date, _
        DateFontSize:=11, _
        TodayButton:=True, _
        BackgroundColor:=RGB(242, 248, 238), _
        HeaderColor:=RGB(84, 130, 53), _
        HeaderFontColor:=RGB(255, 255, 255), _
        SubHeaderColor:=RGB(226, 239, 218), _
        SubHeaderFontColor:=RGB(55, 86, 35), _
        DateColor:=RGB(242, 248, 238), _
        DateFontColor:=RGB(55, 86, 35), _
        SaturdayFontColor:=RGB(55, 86, 35), _
        SundayFontColor:=RGB(55, 86, 35), _
        TrailingMonthFontColor:=RGB(106, 163, 67), _
        DateHoverColor:=RGB(198, 224, 180), _
        DateSelectedColor:=RGB(169, 208, 142), _
        TodayFontColor:=RGB(255, 0, 0), _
        DateSpecialEffect:=fmSpecialEffectRaised)
    

    Here is a small taste of some of the features it includes. All options are fully documented in the userform module itself:

    • Ease of use. The userform is completely self-contained, and can be imported into any VBA project and used without much, if any additional coding.
    • Simple, attractive design.
    • Fully customizable functionality, size, and color scheme
    • Limit user selection to a specific date range
    • Choose any day for the first day of the week
    • Include week numbers, and support for ISO standard
    • Clicking the month or year label in the header reveals selectable comboboxes
    • Dates change color when you mouse over them

提交回复
热议问题