+ Reply to Thread
Results 1 to 11 of 11

Msgbox vbYesNo if then

  1. #1
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Unhappy Msgbox vbYesNo if then

    Hi all,

    I've got a piece of code that takes the user entered responses in a message box to do a set number of things.
    I want it to work in a way that if the user clicks no the sheet shuts down and no further code is run. If the user clicks yes I want the code to continue.

    As it stands, my code shuts down the document irrespective of what is selected by the user. I understand that this is because the shutting down of the workbook happens outside the If statement, but I don’t know how to correct this so that it does what I would like it to do.


    It looks like this at the moment:

    Please Login or Register  to view this content.
    Any help would be greatly appreciated.

    Thanks
    Last edited by Swoootie; 06-24-2014 at 05:46 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Msgbox vbYesNo if then

    Hi, Swoootie,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Msgbox vbYesNo if then

    Now Edited.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Msgbox vbYesNo if then

    Hi, Swoootie,

    maybe like this:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Msgbox vbYesNo if then

    1) Please use code tags as required by forum rules. It makes reading easier. so go back to first post and edit it using code tags as already asked for.
    2) There is missing
    Please Login or Register  to view this content.
    in your code. Presumably it shall be between exit sub and next if (well this second if could be ommitted or replaced by else), so could read rather:
    Please Login or Register  to view this content.
    3) As for the code as it is now - seems that SHEET1.xlsm should be closed only if "No" is pressed. Otherwise only sheet Summary shall be selected (may be would be better to have it Activated instead) and all Check procedure shall stop. If it has been called by its name from Macros (Atl+F8) or is assigned to a button - code shall stop at this point.
    4) PS. APPROACH is neither defined nor assigned value in part of code you shown.
    Last edited by Kaper; 06-24-2014 at 05:51 AM. Reason: took into account what others posted before
    Best Regards,

    Kaper

  6. #6
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Msgbox vbYesNo if then

    Hi Holger,

    This results in an "if without else" error. I think it's because the else is after the exit sub.

    If I remove the Else and revert to my code the Yes option works fine if I remove the exit sub option. If I leave in the exit sub, the yes option does not go to the summary sheet at all.

    I hope this makes sense. Basically I think the Exit Sub and its positioning is causing the issue.

    Thanks for your help.

  7. #7
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Msgbox vbYesNo if then

    In my original code approach and opt were essentially the same thing. I renamed approach to opt and didn't change it before submitting the code. Apologies.

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Msgbox vbYesNo if then

    Hi, Swoootie,

    I think it's because the else is after the exit sub.
    I dounbt that. My sample compiles fine on my pc (Win 8.1, Excel2013) and performs like expected, no error being raised (I used a standard module for placing the code). The code line
    Please Login or Register  to view this content.
    could be passed as the sub would be ended by the closing of ThisWorkbook but would need to stay if you want to close a different workbook (where you should make sure that itīs opened in the same instance of Excel).

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Msgbox vbYesNo if then

    The Exit Sub and the closing of the workbook work as expected when they are run separately but not together (and without the Else statement). The impact of this is that altough the vbNo option works fine the vbYes doesn't and I cannot understand why the code is stumbling here.

    Please Login or Register  to view this content.
    When I put an else after the Exit Sub I get an error. the above does not error, but the vbYes option does not open up the Summary sheet as it should.

    Thanks again.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Msgbox vbYesNo if then

    Hi, Swoootie,

    I would kindly ask you which of my code lines provided doesnīt work as expected as you stick to your original code and apparently have some problems in deciding whether to take a one line If-Statement (no continuation for that check and no End If needed there) or a mulitple line approach (all actions could go into different lines and work for the check just done).

    Please Login or Register  to view this content.
    This code line is doing all the checks for response and all other codelines that follow will work without the check.

    What may help
    Please Login or Register  to view this content.
    And it would be very good practise to turn on the messages before leaving the Sub. Plus I wonder why you would need to turn the messages off if you close the workbook without changes but thatīs another story.

    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    06-10-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Msgbox vbYesNo if then

    Hi Holger,

    As mentioned, I did try your code, however, it wasn't dealing with the else statement and showed an error message. I reverted back to my old code to try and find which part of the code was causing the problem.

    I've included your most recent code into my existing code and although it deals with the closing down and termination of the code itself it does not deal with what to do where the response is a Yes.

    In addition due to security reasons (due to the nature of work that I do), the pieces of code that I have illustrated here are conceptually the same as what I am trying to do, but in reality my code differs. For this reason, only the 2nd half of the code you initially sent over was relevant to what I was attempting.

    Again, thanks for your response. I may just have to find a different workaround as this appears more difficult than I originally thought.

    Thanks

+ 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] Having trouble with msgbox vbYesNo
    By Tayjayt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2013, 12:55 AM
  2. msgbox vbyesno
    By newbielondon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2012, 06:25 PM
  3. MsgBox vbYesNo and Exit Sub
    By Monkeyboyz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-30-2010, 12:46 PM
  4. any way to scroll around the worksheet when a msgbox prompts for a vbyesno choice
    By christheta in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-19-2010, 04:44 PM
  5. VBYesNo MsgBox - Computer always says "Yes"
    By Peter Rooney in forum Excel General
    Replies: 4
    Last Post: 12-22-2005, 10:45 AM

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