Date picker in user form?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a userform. When the user clicks into one of the fields, I'd like a calendar to pop up so that they can pick a date (mainly to avoid dirty data being entered onto the form).

Is this possible? I have Googled and it appears that office 365 doesn't have the functionality (although apparently Office 2010 did?!)

Many thanks for reading.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi,

There is no consistency in the availability of the DatePicker in all Excel versions so personally, I would avoid using it.

There are plenty of “Home Brew” userform versions you can download for free, some use classes others are all VBA in the Userform - any may do what you want.

A more straightforward approach you could consider is create a simple function to test that user has entered a date (in any format) & then when exit the textbox, apply your preferred format

Place in either Standard Module or Forms Code Page

VBA Code:
Function IsNotValidDate(ByVal objTextBox As Object, Optional ByVal DateFormat As String = "dd/mm/yyyy") As Boolean
    With objTextBox
        If Len(.Value) > 0 Then
            IsNotValidDate = Not IsDate(.Value)
            If IsNotValidDate Then
                .Value = ""
                MsgBox "Please enter a valid date.", 48, "Invalid Date Entry"
            Else
                'format date
                .Value = Format(DateValue(.Value), DateFormat)
            End If
        End If
    End With
End Function

To call from your textbox

VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = IsNotValidDate(Me.TextBox1)
End Sub

User should be able to enter a date in any format (25 May 2022) & function should apply your specified format or default format if omitted,

Hope Helpful

Dave
 
Last edited:
Upvote 0
Solution
Hi dmt32,
Great post, thank you for the code, this has helped me loads.

In my Userform I also want to get the user to capture the time, and keep it in format "hh:mm".

How do I do this? Can I manipulate the code and change "Date" to "Time"?
Thanks
S
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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