+ Reply to Thread
Results 1 to 8 of 8

InputBox User Clicked Cancel

  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    InputBox User Clicked Cancel

    Hi all,

    Using Excel 2013.

    How do I trap and what do I return to the Sub when the user clicks Cancel on an Application.InputBox?
    I am trying to get Range Object based on a cell the user clicked on.

    The code currently returns "Object required.." error at Set rng = Application.InputBox( _

    thx
    w

    Please Login or Register  to view this content.
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: InputBox User Clicked Cancel

    Could you not use a sheet specific macro?

    Please Login or Register  to view this content.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: InputBox User Clicked Cancel

    Please Login or Register  to view this content.
    Or just this...
    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-05-2014 at 12:45 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: InputBox User Clicked Cancel

    Thanks AlphaFrog,

    Any other ideas?
    At this point there is nothing on the worksheet, so nothing should be returned at all
    I'm using rng = ws.cells(1,1) but that is not correct.

    If the worksheet is truly empty, I should catch that and tell the user, not return a range.

    thx
    w

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: InputBox User Clicked Cancel

    I don't follow what you're asking.
    What has that got to do with clicking Cancel?

    (I'm stepping away for a couple hours)

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: InputBox User Clicked Cancel

    Hi goss,

    There are several ways to do what you want. When I use 'InputBox' with Type:=8, I like to return the address [rng.address(False,False)] as a string (without the $ signs) rather than the range. This gives me flexibility in processing when the user selects 'Cancel' or a range I don't want to use. I return a string to the calling routine such as 'Illegal Selection', or 'Cancel', that is processed by the calling routine. Of course the calling routine now has to convert the address to a range.

    I hope this helps.

    Lewis

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: InputBox User Clicked Cancel

    Hello goss,

    Whenever a variable is Dimensioned, it is automatically set to default value. For a Range this is the special object Nothing. If the user chooses Cancel on your InputBox which is set to select a Range, the selected Range will be Nothing. All you need to do is trap the error and assign the variable rng as the function's return value. The choises are a user selected range or Nothing.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 10-05-2014 at 02:26 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: InputBox User Clicked Cancel

    Leith,

    Perfect - thanks!

    I let the Function return the Range as Nothing, then I tested for the case of nothing in the Sub().

    thx
    w

+ 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] Repeat Inputbox if user click cancel or if user enter character not in selection criteria
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-24-2014, 02:44 AM
  2. [SOLVED] GetOpenFile Crashes upon cancel being clicked
    By AdamManning in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2014, 07:09 AM
  3. [SOLVED] Looping sub not detecting user cancel from inputbox
    By jshaw82 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2013, 06:14 PM
  4. How do i edit what happens when cancel is clicked in an inputbox. NEWBIE NEEDS HELP!
    By 10121730 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2012, 03:59 PM
  5. Cancel Macro is user selects 'cancel' at save menu
    By Mark in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2005, 01:06 PM

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