Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: Exit Macro if NO is choosen from Msgbox

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    359
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I have this code.

    Dim ws As Worksheet

    For Each ws In Sheets
    If ws.Name = "Test" Then
    ws.Select
    Application.DisplayAlerts = False
    Message = MsgBox("Delete " & ws.Name & " sheet?", vbYesNo, "ARE YOU SURE ABOUT THIS?")
    'Does not require End If statement
    If Message = vbYes Then Worksheets(ws.Name).Delete
    End If
    Next ws

    - more code-

    End Sub

    How do I get it to exit the macro if the use chooses NO instead of YES?

  2. #2
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default


    If Message = vbYes Then
    Worksheets(ws.Name).Delete
    else
    exit sub 'or STOP if you have additional subroutines running
    End If



  3. #3
    Board Regular
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    359
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I am afriad I don't know what you mean by additional sub-routines!

    Please excuse my ignorance!

  4. #4
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Christchurch New Zealand
    Posts
    1,030
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    or
    if message = vbno then exit sub

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •