Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

msgbox - cancel sub when selecting "No"

Latest post Thu, Dec 11 2008 10:52 AM by Gila. 7 replies.
  • Wed, Dec 10 2008 1:01 PM

    • Gila
    • Top 50 Contributor
    • Joined on Fri, Nov 21 2008
    • Posts 34
    • Points 553

    msgbox - cancel sub when selecting "No"

    Need help finishing this code for a msgbox. If no is selected I want the Sub to End/Cancel, If yes is selected Sub will run its course. I have found plenty of info on making a msgbox but none show how to end the sub when no is selected.

    Sub Insert_Row()

    Dim Response

    Response = MsgBox("Are Cells Dated?", vbYesNo, "Yes or No")
    If Response = vbYes Then
    Else
    If Response = vbNo Then

    End If
    End If

        Sheets("Sheet1").Activate
        Rows("5:5").Select
        Selection.Copy
        Sheets("Sheet2").Select
        Rows("3:3").Select
        Selection.Insert    

    Range("C7").Select
       
    End Sub 

    Filed under:
    • Post Points: 21
  • Wed, Dec 10 2008 1:36 PM In reply to

    Re: msgbox - cancel sub when selecting "No"

    I use Select Case:

    Sub Insert_Row()
    Select Case MsgBox("Are Cells Dated?", vbYesNo, "Yes or No")
        
        Case Is = vbYes
            'Do your code here
            Sheets("Sheet1").Activate
            Rows("5:5").Select
            Selection.Copy
            Sheets("Sheet2").Select
            Rows("3:3").Select
            Selection.Insert
            Range("C7").Select
        
        Case Is = vbNo
            MsgBox "Action Cancelled"

    End Select
    End Sub

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 21
  • Wed, Dec 10 2008 3:12 PM In reply to

    • Gila
    • Top 50 Contributor
    • Joined on Fri, Nov 21 2008
    • Posts 34
    • Points 553

    Re: msgbox - cancel sub when selecting "No"

    Excellent Mike!  Thank you!

    What is the difference between (Dim Responses) and using (Case)? I’ll have to do some research on this. Thanks again for the help.

     

    • Post Points: 37
  • Wed, Dec 10 2008 7:00 PM In reply to

    Re: msgbox - cancel sub when selecting "No"

    It is a style thing Gila, yours can work just as well if code correctly

     

    Sub Insert_Row()
    Dim Response As Long

        Response = MsgBox("Are Cells Dated?", vbYesNo, "Yes or No")
        If Response = vbYes Then
       
            Sheets("Sheet1").Rows("5:5").Copy
            Sheets("Sheet2").Rows("3:3").Insert
        Else
       
            'do nothing
        End If
      
    End Sub

    Note that I declared a data type for Response, and I removed all the selecting for the row copy, to make it leaner, meaner and more efficient.

    Regards

    Bob

    • Post Points: 5
  • Thu, Dec 11 2008 3:57 AM In reply to

    Re: msgbox - cancel sub when selecting "No"

    Bob's right, it's a style thing.

    The only thing I would point out.  Using Case, you don't have to create a variable for the response.   

     

     

     

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 21
  • Thu, Dec 11 2008 4:11 AM In reply to

    Re: msgbox - cancel sub when selecting "No"

    That is not Case doing that, it is just another style thing


    Sub Insert_Row()

        If MsgBox("Are Cells Dated?", vbYesNo, "Yes or No") = vbYes Then
     
            Sheets("Sheet1").Rows("5:5").Copy
            Sheets("Sheet2").Rows("3:3").Insert
        Else
     
            'do nothing
        End If
     
    End Sub

    and similarly, Case can use a variable just as well


    Sub Insert_Row()
    Dim Response As Long

        Response = MsgBox("Are Cells Dated?", vbYesNo, "Yes or No")
        Select Case Response
       
            Case vbYes
      
                Sheets("Sheet1").Rows("5:5").Copy
                Sheets("Sheet2").Rows("3:3").Insert
       
            Case Else
      
                'do nothing
        End Select
     
    End Sub


    But I would argue that, from a style (which ultimately means coding practice and maintainability) perspective, it is better to create a variable and capture the MsgBox response rather than embed it in the conditional logic.

    Regards

    Bob

    • Post Points: 21
  • Thu, Dec 11 2008 6:38 AM In reply to

    Re: msgbox - cancel sub when selecting "No"

    You could just modify one line of the original code to read:

      If Response = vbNo Then Exit Sub

    Of course, this is not always desirable but can be useful for a quick fix...

    I would normally use something similar to Bob's second example.

    • Post Points: 21
  • Thu, Dec 11 2008 10:52 AM In reply to

    • Gila
    • Top 50 Contributor
    • Joined on Fri, Nov 21 2008
    • Posts 34
    • Points 553

    Re: msgbox - cancel sub when selecting "No"

    Thank you all, this is very good information and explanations.

    • Post Points: 5
Page 1 of 1 (8 items) | RSS
Copyright Excel User Group and the relevant contributors, 2010. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.