+ Reply to Thread
Results 1 to 7 of 7

MsgBox Not Exiting Sub as it Should.

  1. #1
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    MsgBox Not Exiting Sub as it Should.

    So I have a Userform called ufMoral and a TextBox on the Userform called tb1 and I need to restrict the values for this Textbox to 3 letters only.
    S, N, or U

    Here is my code:
    Please Login or Register  to view this content.
    My problem is that when the Error MsgBox is triggered I have to click Ok on the MsgBox 3 times before it closes. I am open to any suggestions on how I can do this better or how I can fix this problem. All help is greatly appreciated!
    Thanks!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: MsgBox Not Exiting Sub as it Should.

    Posting code like that without providing the context (How it's called/Event handler etc) is pointless... and there's no reason (in the code you have posted) that the MsgBox will be displayed 3 times.

    As it seems that only 'S', 'N' or 'U' are valid entries, a ComboBox with those 3 options in the list and the style set to DropDownList (User cannot type anything, only select from the list) would get rid of a lot of issues for you.

  3. #3
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358
    Quote Originally Posted by cytop View Post
    Posting code like that without providing the context (How it's called/Event handler etc) is pointless... and there's no reason (in the code you have posted) that the MsgBox will be displayed 3 times.

    As it seems that only 'S', 'N' or 'U' are valid entries, a ComboBox with those 3 options in the list and the style set to DropDownList (User cannot type anything, only select from the list) would get rid of a lot of issues for you.
    Sorry, the code is located in the change event for tb1. I considered a listbox but for my own reasons i much prefer to stay with textbox if possible.
    What i am trying to accomplish is when a user enters a forbidden value, (anything except S, N, or U) a msgbox should popup and warn the user that only those 3 values are allowed. When the user clicks ok on the msgbox it should disappear, tb1.text should be set to "" to remove the forbidden value, and then it should exit sub. However as soon as the msgbox comes up, you have to hit the ok button 3 times before the msgbox disappears and the exit sub happens.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: MsgBox Not Exiting Sub as it Should.

    Hi unit285

    As you may be aware, Application Enable Events Code doesn't work in UserForms so you need to create other Error Trapping Methods. Easily done. It's perhaps why the Change Event Code fires multiple times.

    Not enough information to apply.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,012

    Re: MsgBox Not Exiting Sub as it Should.

    Here is a Select Case method. Hopefully you can apply it to your situation:

    One userform, one textbox, one commandbutton. Change the control names as required.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: MsgBox Not Exiting Sub as it Should.

    Hi

    What are the reasons for using a text box over combo box as cytop suggested? They occupy the same real estate on screen and the combo can remove your issue.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: MsgBox Not Exiting Sub as it Should.

    Quote Originally Posted by Logit View Post
    Here is a Select Case method. Hopefully you can apply it to your situation:

    One userform, one textbox, one commandbutton. Change the control names as required.

    Please Login or Register  to view this content.
    Thanks a lot for this response! As I was reading the code you posted an idea spawned which ended up solving my problem. The solution was to change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

+ 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. Macro to capture MsgBox prompt to a string variable but ignore the MsgBox
    By BuglerDobbs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2015, 10:56 AM
  2. [SOLVED] Problem with VBA Editor uses lower case on some lines (ex. msgbox instead of MsgBox)
    By stubbsj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-14-2013, 06:59 PM
  3. [SOLVED] Exiting Sub
    By zhb12810 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-22-2012, 06:22 PM
  4. [SOLVED] VBA msgbox when exiting Excel workbook
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-03-2012, 04:27 AM
  5. Exiting a Sub
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2010, 04:13 PM
  6. Message when exiting
    By marcellosanto in forum Excel General
    Replies: 1
    Last Post: 02-03-2008, 03:41 PM
  7. MsgBox-center the message on a msgbox
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2007, 06:48 AM

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