How to Accept Valid Dates Only From User in Excel VBA?
Tags: toacceptvaliddateson, vbamacros
Published: 2021-09-13 06:16:05

---
title: How to Accept Valid Dates Only From User in Excel VBA?
published: true
date: 2021-09-07 12:53:50 UTC
tags: ToAcceptValidDatesOn,VBAMacros
canonical_url: https://geekexcel.com/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](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](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-8-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- 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

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-18.png)<figcaption>Save the Code</figcaption>

- 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.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-9-1024x184.png)<figcaption>Choose Macro option</figcaption>

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

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-8.png)<figcaption>Run the Code</figcaption>

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

![](https://geekexcel.com/wp-content/uploads/2021/09/type-date.png)<figcaption>Type Date</figcaption>

- 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.

![](https://geekexcel.com/wp-content/uploads/2021/09/inavlid-date.png)<figcaption>Dialog box</figcaption>

## 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](https://geekexcel.com/)!!**

**Keep Reading:**

- **[Excel Formulas to Show Checkmark for Valid Inputs ~ Simple Tutorial!!](https://geekexcel.com/excel-formulas-to-show-checkmark-for-valid-inputs/)**
- **[How to Set Data Validation To Accept Only IP Addresses in Excel?](https://geekexcel.com/how-to-set-data-validation-to-accept-only-ip-addresses-in-excel/)**
- **[How to Modify and Manage Multiple Cell Comments in Excel Office 365?](https://geekexcel.com/how-to-modify-and-manage-multiple-cell-comments-in-excel-office-365/)**
- **[How to Remove/Add Author Name in Comments in Excel 365?](https://geekexcel.com/how-to-remove-add-author-name-in-comments-in-excel-365/)**
- **[Excel Formulas to Create Date Range From Two Dates ~ Quick Tricks!!](https://geekexcel.com/excel-formulas-to-create-date-range-from-two-dates-quick-tricks/)**