DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to Accept Valid Dates Only From User in Excel VBA?

In this article, we will talk about how to accept valid dates only from users in Excel VBA by using the formula and user-defined function in VBA. Let’s see them below!! Get an official version of ** MS Excel** from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Example

  • Firstly, in the Excel Worksheet, you have to go to the Developer Tab.
  • Then, you need to ** ** select the Visual Basic option under the Code section.

Select Visual Basic
Select Visual Basic

  • Now, you have to copy and paste the code given below.
Sub check_date()
Dim dob As Date
On Error GoTo Errorhandler
dob = Application.InputBox("Enter Your Date of Birth")
On Error GoTo 0
Debug.Print dob
Exit Sub

Errorhandler:
MsgBox "Please enter a valid date."
End Sub

Enter fullscreen mode Exit fullscreen mode
  • After that, you need to save the code by selecting it and then close the window.

Save the Code

  • Again, you have to go to the Excel Spreadsheet , and click on the Developer Tab.
  • You need to choose the Macros option in the Code section.

Choose Macro option

  • Then, you have to make sure that your macro name is selected and click the *Run * button.

Run the Code

  • Now, you will receive an input ** dialog box, and then you need to **type date in the text box.

Type Date

  • After that, if you enter a valid date , it will accept as dates from users.
  • Finally, if you don’t enter any date , then you will receive display dialog box as given below.

Dialog box

Wrap-Up

Here, we have explained the step-by-step procedure to accept valid dates only from users in Excel VBA. Make use of this. Please share your worthwhile feedback in the below comment section. To learn more, check out our website Geek Excel!!

Keep Reading:

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please show some love ❤️ or share a kind word in the comments if you found this useful!

Got it!