Input Box Exit Sub If Not Date or When Cancel is Selected.

ajlcpa

New Member
Joined
Nov 18, 2011
Messages
15
Hi Folks:

I can handle this if I Dim the variable as a string, but I really need to Dim it as a Date. I want to exit the sub if a date is not entered or if Cancel is selected.

Thank you for reading and your help.


VBA Code:
 Dim DateEntry As Date
    
   
   DateEntry = InputBox("Please enter the oldest start date using this format MM/DD/YY", "Date Selection")
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Maybe this:
VBA Code:
 Dim DateEntry As Date, v As String
    v = Application.InputBox("Please enter the oldest start date using this format MM/DD/YY", "Date Selection", Type:=2)
    If v Like "##-##-##" Then
        DateEntry = CDate(v)
    Else
        Exit Sub
    End If
 
Upvote 0
Maybe this:
VBA Code:
 Dim DateEntry As Date, v As String
    v = Application.InputBox("Please enter the oldest start date using this format MM/DD/YY", "Date Selection", Type:=2)
    If v Like "##-##-##" Then
        DateEntry = CDate(v)
    Else
        Exit Sub
    End If
Thank you for the quick response. I will try, and let you know. Very much appreciated.
 
Upvote 0
Thank you. I would love to try date picker. I need to research how to access. Thank you.
You are welcome. I think that link I provided should have the details you need for that.
 
Upvote 0
Sorry, the pattern is wrong, it should be:
If v Like "##/##/##" Then
 
Upvote 0
Thank you. I am very eager to try this I will let you know shortly.
Hello: After server attempts, I couldn't get it work. It exited the sub nicely, but didn't execute the code if entered a correctly formatted date. I am sure it is me. Thank you very much though.
 
Upvote 0
1. Can you show us your code?
2. What would you do after you assign the date to DateEntry ?

I tried it like this:
run the code below > type 12/04/21 > OK
a message box show up, showing: 12/04/2021

see that 21 become 2021, so DateEntry is a date
note: my regional setting use "day-month-year" not "month-day-year"
VBA Code:
Sub tryDate()
 Dim DateEntry As Date, v As String
    v = Application.InputBox("Please enter the oldest start date using this format MM/DD/YY", "Date Selection", Type:=2)
    If v Like "##/##/##" Then
        DateEntry = CDate(v)
    Else
        Exit Sub
    End If
    MsgBox DateEntry
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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