+ Reply to Thread
Results 1 to 8 of 8

How to check if user clicks on Ok or Cancel in inputbox?

  1. #1
    Registered User
    Join Date
    06-12-2021
    Location
    India
    MS-Off Ver
    2013
    Posts
    5

    How to check if user clicks on Ok or Cancel in inputbox?

    If a user clicks on 'Cancel' in Inputbox it sends an empty string "". Similarly, if the user clicks on "ok" without entering any input it also sends an empty string"". So how can you detect if the user has clicked "ok" without entering input or cancel on inputbox?

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    lets call your variable myString

    Cancel can be detected by
    Please Login or Register  to view this content.
    OK with an empty string can be detected by
    Please Login or Register  to view this content.
    <<< If you have valued anyone's contributions in this thread, please click * to thank them for their efforts

  3. #3
    Registered User
    Join Date
    06-12-2021
    Location
    India
    MS-Off Ver
    2013
    Posts
    5

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    Thank you for your response AskMeAboutExcel. I tried your suggestions, however, when I click "Cancel" now, the check for VbNullString is fulfilled and the code below this condition is executed.

    Here's a snippet of my code:

    Please Login or Register  to view this content.
    When I click cancel, the msgbox is still shown (It should be shown when I click OK)

    Why is this happening?

    Edit: When I change the order of the elseif statements, it works. The code is:

    Please Login or Register  to view this content.
    But the order of the elseif shouldnt matter right? Whats wrong here?
    Last edited by aks2161989; 04-09-2022 at 12:59 AM.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    Use InputBox method
    e.g

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-12-2021
    Location
    India
    MS-Off Ver
    2013
    Posts
    5

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    Thank you jindon. I tried your suggestions but now nothing happens when I click cancel. it keeps showing the inputbox

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    You asked
    Quote Originally Posted by aks2161989 View Post
    If a user clicks on 'Cancel' in Inputbox it sends an empty string "". Similarly, if the user clicks on "ok" without entering any input it also sends an empty string"". So how can you detect if the user has clicked "ok" without entering input or cancel on inputbox?
    So, I replied.

    What are you trying to do?

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    See if you can adapt this structure to your needs...
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: How to check if user clicks on Ok or Cancel in inputbox?

    Quote Originally Posted by aks2161989 View Post
    But the order of the elseif shouldnt matter right? Whats wrong here?
    If you press cancel, StrPtr(iBox) = 0 and iBox = vbNullString are both true.
    If you press ok with no input, only iBox = vbNullString is true

    Therefore if you put vbNullString as the first elseif test, it will always run that logic, and not test StrPtr(iBox) = 0

+ 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] How to detect if user select cancel InputBox VBA MS Access
    By kirana2014 in forum Access Tables & Databases
    Replies: 1
    Last Post: 05-08-2021, 12:41 PM
  2. [SOLVED] delete specific cell value if user clicks on cancel in inputbox
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2020, 12:54 AM
  3. Replies: 4
    Last Post: 02-09-2016, 03:23 PM
  4. [SOLVED] MACRO: If user clicks cancel in inputbox,then do nothing. Problem with inputbox appearance
    By Tona in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-05-2015, 04:33 AM
  5. [SOLVED] InputBox User Clicked Cancel
    By goss in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-05-2014, 03:19 PM
  6. [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
  7. [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

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