+ Reply to Thread
Results 1 to 7 of 7

InputBox Cancel causes type mismatch

  1. #1
    Registered User
    Join Date
    09-29-2015
    Location
    Golden, CO
    MS-Off Ver
    Professional 2013
    Posts
    4

    InputBox Cancel causes type mismatch

    Hi everyone,

    I'm trying to allow my subroutine to be able to handle a user choosing cancel, but I'm not sure how to write an if statement to do so when the inputbox is formatted as double rather than a string. I've tried using both "" or 0 to indicate cancel, but neither works. each time I get a type mismatch error. Any thoughts?

    GasGravity = InputBox("prompt", "title", 0#)
    If (GasGravity = 0#) Then
    MsgBox "Please input a specific gravity to proceed", vbOKOnly, "Error"
    GasGravity = InputBox("What Gas Gravity would you like to use?", "Gas Gravity", 0#)
    ElseIf (GasGravity = "") Then
    MsgBox ("User Has Cancelled")
    Exit Sub
    End If

    I know this is probably simple, I'm very new at this. Thanks for your help!

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: InputBox Cancel causes type mismatch

    One way:

    Please Login or Register  to view this content.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Registered User
    Join Date
    09-29-2015
    Location
    Golden, CO
    MS-Off Ver
    Professional 2013
    Posts
    4

    Re: InputBox Cancel causes type mismatch

    Still didnt work...

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: InputBox Cancel causes type mismatch

    Still didnt work...
    Works just fine for me. What are you doing wrong?

  5. #5
    Registered User
    Join Date
    09-29-2015
    Location
    Golden, CO
    MS-Off Ver
    Professional 2013
    Posts
    4

    Re: InputBox Cancel causes type mismatch

    I'm not sure. I noticed you added a couple things to the code I copied, but there's quite a bit more to it that I didn't copy. Really it's just one small issue-- when I click cancel without inputting a number it errors out. However, if I use your code from above and modify it a little bit to see if it works, it's fine. What I did was change Len(GasGravity)=0 to Len(GasGravity)=4 and input a 4 digit number. This gave me the expected result. So all I need is a way to reference the blank value returned when clicking cancel. To me it seems to be a problem with having the inputbox working with numbers rather than a string. Unfortunately that's something I can't change due to other parts of the code. What is the specific value returned with cancel?

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: InputBox Cancel causes type mismatch

    To me it seems to be a problem with having the inputbox working with numbers rather than a string
    Did you properly follow my coding and dim GasGravity as Variant? If you Dim as long/double/etc it will error. It will probably also work dim as string but variant will be best in this situation.

    Please Login or Register  to view this content.
    Last edited by stnkynts; 09-29-2015 at 11:49 PM.

  7. #7
    Registered User
    Join Date
    09-29-2015
    Location
    Golden, CO
    MS-Off Ver
    Professional 2013
    Posts
    4

    Re: InputBox Cancel causes type mismatch

    That was definitely the problem. Works Perfectly now. Thank you!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] inputbox cancel
    By jacobsoo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2013, 07:03 AM
  2. InputBox and Cancel
    By michaelaindia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2012, 04:32 AM
  3. [SOLVED] Cancel my Inputbox
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2012, 11:05 AM
  4. [SOLVED] Input Box must be integer, Cancel button type mismatch
    By Nrowell92 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-30-2012, 03:29 PM
  5. Using Application.Inputbox.........getting type mismatch error
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2011, 07:33 AM
  6. Type Mismatch error on InputBox
    By h_aesa1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2009, 09:21 AM
  7. [SOLVED] Type Mismatch Error when using InputBox Method
    By Anolan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2005, 03:40 PM
  8. InputBox - Cancel
    By Lonwez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2005, 07:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1