Search code examples
vbaexceldatepickertextbox

Identify Week Number, Month Number, Year from DatePicker control in VBA userform


I have been trying to make my user form work by getting codes everywhere. I don't have any experience in VBA so the code that I have are patches from different sources.

I have a datepicker control in my user form and i'd like to get the corresponding week number, month number and year based on the date that is chosen from the datepicker control box and populate the corresponding textboxes for each

datepicker and textbox dependencies.


Solution

  • To get the week number use the below. The third and fourth arguments allow you to dictate the first day/first week of the year.

    TextBoxWeek.Value =  DatePart("ww", TextBoxDatePicker.Value, vbMonday, vbFirstJan1 )
    

    Month:

    TextBoxMonth.Value = format(TextBoxDatePicker.value,"mm")
    

    Year: (you can also use "yy" to get the shorthand date eg. '17' rather than '2017')

    TextBoxYear.Value = format(TextBoxDatePicker.value, "yyyy")