Runtime error 13 Type Mismatch on an Inputbox value

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VBA Code:
EmailTo:
    
    If EmailTo = "" Then
        MsgBox "Uh Oh! " & fName & "," & vbNewLine & "It appears your email correspondents are empty. Let's take care of that now.", vbCritical, "No emails listed to send report to"
        EmailEntryNum = InputBox(fName & "," & vbNewLine & "How many people will you need to send this attachment to? Enter that number below.")
        If IsNumeric(EmailEntryNum) And EmailEntryNum > 0 Then
            MsgBox "YES!" 'For Test Purposes Only
        Else
            MsgBox "AWE! You either did not enter a number or your number was less than 1. Let's try this process this again."
            GoTo EmailTo
        End If
    Else
        'vbYes=6 vbNo=7
        WkAnswer = MsgBox(Greetings & fName & vbNewLine & vbNewLine & "Is this for the current week?" & vbNewLine & vbNewLine & "Select: " & vbNewLine & "Yes = Current Week" & vbNewLine & "No = Previous Week", vbDefaultButton1 + vbYesNo)

I declared "EmailEntryNum" as an integer. So, what I am wanting is if the user enters ANYTHING besides a number, it will jump to the else statement. It sounds simple but I can't get it to work. Here is where the error occurs.
VBA Code:
 EmailEntryNum = InputBox(fName & "," & vbNewLine & "How many people will you need to send this attachment to? Enter that number below.")
So this code will run but as stated previously, if I enter anything besides a number, I get a runtime error 13 type mismatch. Thank you.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I declared "EmailEntryNum" as an integer.
By doing this, if they enter anything other than in Integer, it will result in an immediate error and never make it to your IF checks!

You have a few different options:
1. Declare it as "Variant" instead, which will accept anything, then you can do your checks,
- or -
2. Add Error Handling Code to capture and handle what happens when they make an entry that is not an Integer.
 
Upvote 1
Solution
Thank you for the suggestions. I usually don't like setting variables to variant but in this case that did the trick. Thank you.
 
Upvote 0
You are welcome!

I agree, I usually don't like it either. However, with input values where you never know what is coming in, if you set it to something other than "Variant", then you need to handle the error situation which arises if they enter in something not valid.

One way or the other, you need to deal with handling bad entries.
 
Upvote 0
Could I not "force" the user to use the correct type? I haven't tried this yet but replace the line of code that shows
VBA Code:
If IsNumeric(EmailEntryNum) And EmailEntryNum
with
VBA Code:
If err.number = 13 then 
msgbox "Please only enter numbers"
Thank you again
 
Upvote 0
Forgot to add "and put it in a loop" and hopefully that will "train" the user on what values to put in.
 
Upvote 0
Typically, you would set up error handling code like this:
VBA Code:
On Error GoTo err_handler
' The section of the code that returns goes here
On Error GoTo 0

'The rest of your code


'Put this just before the end of your code:
Exit Sub

err_handler:
    If err.Number = 13 Then
        MsgBox "Please only enter numbers!", vbOkOnly, "ERROR: TRY AGAIN!"
    Else
        MsgBox err.Number & ":" & err.Description
    End If

End Sub
 
Upvote 0
Could I not "force" the user to use the correct type? I haven't tried this yet but replace the line of code that shows
As an alternative you could try something like the below (untested)

VBA Code:
Sub Integer_Only()
    Dim myNum As Integer, myInput As String
    
Restart:
    myInput = InputBox("Enter an integer:")
    
    If myInput = "" Then
        MsgBox "Good Bye", 64, "Action Cancelled"
        Exit Sub
    ElseIf IsNumeric(myInput) Then
        If CDbl(myInput) > Int(myInput) Then
            MsgBox "You entered a Decimal" & vbCrLf & "Try Again", 64, "Integers Only"
            GoTo Restart
        Else
           myNum = myInput
        End If
    Else
        MsgBox "Integers Only" & vbCrLf & "Try Again", 64, "Integers Only"
        GoTo Restart
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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