getting type mismatch error when cancel button is clicked on inputbox

GregJG

Board Regular
Joined
Jun 28, 2004
Messages
166
when i click the cancel button of my inputbox i get error "13" type mismatch.

I have tried correcting by using the code below with many variations but can't get it to stop. anyone know how?

Code:
    Dim Ending As Date
    Ending = InputBox("enter date", Title:="INPUT - Date")
    If CDate(Ending) = False Then
        MsgBox "You pressed cancel!"
    End If
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Ending is a date, InputBox returns a string. That is the mismatch.

Code:
Dim UserInput as String
Dim Ending As Date

UserInput = InputBox("enter date", Title:="INPUT - Date")
If StrPtr(UserInput) = 0 Then
   MsgBox "Cancel Pressed"
   Exit Sub
End If

If IsDate(UserInput) then
   Ending = CDate(UserInput)
Else
   MsgBox "Bad data entry"
   Exit Sub
End If
 
Upvote 0
ahh, I see what you mean mike. another note I need to add to my list of many

Thanks !!!
 
Upvote 0
You could, alternatively, use the Inputbox method as opposed to the function version. The following will work successfully:

Code:
 Dim Ending As Date
    Ending = Application.InputBox("enter date", Title:="INPUT - Date", Type:=2)
    If CDate(Ending) = False Then
        MsgBox "You pressed cancel!"
    End If
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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