+ Reply to Thread
Results 1 to 5 of 5

Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

    Hi all,
    I want to incorporate message box at the beginning of a long code.

    Message box has 'YES' and 'NO'

    If answer is yes then it should continue with further routine.

    If answer is NO then :
    1. It should get message box ' Go to sheet2'
    when pressed OK then sub is exited
    2. and sheet2 activates

    This is repeated until the answer is YES.
    Code
    Sub Msgbox_Yes_No()
          Dim Response As Integer
          Response = MsgBox(prompt:="Have you done Defib Checks First?'.", Buttons:=vbYesNo)
         If Response = vbYes Then
          MsgBox " Thanks"
          
          Else
          Do
           ' I need help here 
    MsgBox " Go to Sheet2"
    This workbook.Sheets ("Sheet2").activate
    This workbook.Sheets ("Sheet2").visible     
    
     Loop While Answer = vbYes
          
    End If
    
    
       End Sub
    Any help please
    Thanks

    kind regards

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

    You don't need the loop.
    Sub Msgbox_Yes_No()
    Dim Response As Integer
    
        Response = MsgBox(prompt:="Have you done Defib Checks First?'.", Buttons:=vbYesNo)
        
        If Response = vbYes Then
            MsgBox " Thanks"
            Exit Sub
        Else
            ' I need help here
            MsgBox "Go to Sheet2"
            ThisWorkbook.Sheets("Sheet2").Visible = True
            ThisWorkbook.Sheets("Sheet2").Activate
    
        End If
    
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

    Hi Norie,
    Perfect solution.
    Thanks for your prompt response and Kind help.
    It goes to sheet2 verywell if answer is NO
    However since messagebox routine is part of a big code, it still triggers the following which is the next action in the big code.
    How to stop it please?

      Call Msgbox_Yes_No
          
    Do
        eADDR = Application.InputBox("Enter email address", _
    I have clicked* to thank you. I will close this as solved subsequently.
    Kind regards

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

    If you click No in the mesage box the sub Msgbox_Yes_No will be exited, that's what this line does.
    Exit Sub
    If you want to totally stop code execution I would suggest putting the code for Msgbox_Yes_No inside the main sub.
    Last edited by Norie; 01-19-2014 at 06:04 PM.

  5. #5
    Forum Contributor
    Join Date
    02-19-2013
    Location
    UK
    MS-Off Ver
    Excel 2019
    Posts
    677

    Re: Message box if Yes continue with subroutine and if NO then exit sub and open sheet2

    Hi Norie,
    Thanks again.
    It was a great help from you.
    A learning curve for me.
    Thanks again.
    Kind regards

+ 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. Write a subroutine in a module to change case upon exit of a cell to upper case
    By Stephd22 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-15-2013, 06:39 PM
  2. [SOLVED] Display Pop Up Message while a Subroutine Executes and close when Exit Sub
    By stubbsj in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-02-2012, 02:45 AM
  3. Exit Private Subroutine
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2010, 01:23 AM
  4. all objects and userforms opened in a subroutine closed upon exit by default?
    By stanigator in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-11-2007, 09:58 PM
  5. How do I exit a macro subroutine?
    By John in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-14-2006, 10:15 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