Use the DatePicker dialog for simple and user friendly selection of a date in VBA macros user interaction

DatePicker dialog

The easiest and most common way to interact with users when making VBA macros is by using InputBox and MessageBox. This article introduces an additional dialog, the DatePicker. The DatePicker allows the users to specify a date either by typing it in the textbox, or by using a calendar (to open click popup button). The DatePicker dialog contains a DTPicker control and Ok and Cancel buttons.

DatePicker to allow the user to select a date

The code below shows how to open the DatePicker dialog with the listbox filled with sample values allowing the user to select one value only, and print the selected value - if any.

Dim datEmpty As Date, datPicked As Date
datPicked = PickDate(BeginDate:=#4/15/2021#, EndDate:=#4/15/2024#)
If datPicked > datEmpty Then
    Debug.Print datPicked
End If

Inserting the DatePicker using Code VBA

The DatePicker modules are included in Code VBA - download free trial here. When you select the appropriate way to call the DatePicker included in the menu, see image below:

  1. The selected call fragment is inserted - see code above.
  2. The implementation modules modDatePicker and FormDatePicker are added to the VBA Project.
  3. A reference is set to the Microsoft Forms 2.0 Object Library.
insert DatePicker dialog using Code VBA