InputBox Cancel With Default Value

martipe1

Board Regular
Joined
Mar 16, 2007
Messages
71
I'm asking for 2 dates for a report, the default date for start date is Today() and for end date is start date, I want to exit the sub if the user cancels, but keep getting error Type mismatch. Already have tried to check for False, "" and StrPtr with no success.
VBA Code:
StrDate= InputBox("Report start date? (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy"))

    If StrPtr(StrDate) = 0 Then
        Exit Sub
    End if

EndDate= InputBox("Report end date? (dd/mm/aa)", "Report", Format(StrDate, "dd/mm/yy"))

    If EndDate = "" Then
        Exit Sub
    End if

Any idea.

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm asking for 2 dates for a report, the default date for start date is Today() and for end date is start date, I want to exit the sub if the user cancels, but keep getting error Type mismatch. Already have tried to check for False, "" and StrPtr with no success.
VBA Code:
StrDate= InputBox("Report start date? (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy"))

    If StrPtr(StrDate) = 0 Then
        Exit Sub
    End if

EndDate= InputBox("Report end date? (dd/mm/aa)", "Report", Format(StrDate, "dd/mm/yy"))

    If EndDate = "" Then
        Exit Sub
    End if

Any idea.

Thanks!
Hi,
Try this.
VBA Code:
StrDate= InputBox("Report start date? (dd/mm/aa)", "Report", Format(Now(), "dd/mm/yy"))

    If StrDate = vbNullString or Not (IsDate(StrDate)) then
        Exit Sub
    End if

EndDate= InputBox("Report end date? (dd/mm/aa)", "Report", Format(StrDate, "dd/mm/yy"))

    If EndDate = vbNullString or Not (IsDate(EndDate))  Then
        Exit Sub
    End if

Let me know if it works for you ?.
 
Upvote 0
Thank you very much for your answer.

Unfortunately, your suggestion didn't work, same error Run time error 13: Type mismatch and it takes me to the StrDate expression.

As a side note, I check later if the date is valid.

Thanks.
 
Upvote 0
Have you declared your variables? If so what are they?
 
Upvote 0
In that case change them both to Variant
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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