none
excel 2010 Message Box 101 RRS feed

  • Question

  • Nube question I'm sure. I have a message box at the top of my Sub, but either answer is allowing the code to run which tells me its a structure issue. Pretty simple stuff so Im sure I dont have it set up right. I want the Answer "no" to stop the remaining routine from running and basically wait for the next button click and stay in that loop until the answer becomes "yes" allowing the remaining routing to run.

    Thanks

    Private Sub CommandButton1_Click()

    If MsgBox("My message for user.", vbOKCancel) = vbOK _
        Then
            MsgBox "Proceed, OK"
        Else
           MsgBox "you cancelled" 


    heads up

    Friday, October 25, 2013 4:11 PM

All replies

  • You'd use Exit Sub to stop further execution:

    Private Sub CommandButton1_Click()
        If MsgBox("My message for user.", vbOKCancel) = vbOK Then
            MsgBox "Proceed, OK"
        Else
            MsgBox "you cancelled"
            Exit Sub
        End If
        ...
        ...
    End Sub
    

    Another way would be to place all code between If ... Then and Else:

    Private Sub CommandButton1_Click()
        If MsgBox("My message for user.", vbOKCancel) = vbOK Then
            MsgBox "Proceed, OK"
            ' all further code here
            ...
            ...
        Else
            MsgBox "you cancelled"
        End If
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, October 25, 2013 7:21 PM