+ Reply to Thread
Results 1 to 2 of 2

Problems with "Exit Sub"

Hybrid View

  1. #1
    Registered User
    Join Date
    09-14-2010
    Location
    Germany
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Problems with "Exit Sub"

    Hi everyone!

    I have the following VBA code:

    Sub UnlockPump()
    MsgBox "This step can not be redone. Are you sure to proceed?", vbQuestion + vbOKCancel
    If Answer = vbCancel Then Exit Sub
    Application.ScreenUpdating = False
    Sheet1.Unprotect
        Range("P33:Q33").Select
        Selection.Locked = False
        Selection.FormulaHidden = False
        Selection.ClearContents
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Sheet1.Protect
    Application.ScreenUpdating = True
    End Sub
    I am beginner with VBA. All of the code which starts with a space I have recorded from a macro. The other lines I found on the web and copy paste it.

    The problem about this code is that after the message box pops up, the sub does not end after clicking on cancel. So no matter that you click the macro just goes on.

    Hope that someone can help me here.

    Thanks!
    Last edited by wishny; 09-14-2010 at 07:10 PM. Reason: editing CODE

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Problems with "Exit Sub"

    You didn't return the result of MsgBox to the variable:
    Option Explicit
    
    Sub UnlockPump()
        Dim iAnswer     As Integer
    
        iAnswer = MsgBox(Prompt:="This step can not be redone. Are you sure to proceed?", _
                         Buttons:=vbQuestion + vbOKCancel)
        If iAnswer = vbOK Then
            Sheet1.Unprotect
            With Range("P33:Q33")
                .Locked = False
                .FormulaHidden = False
                .ClearContents
                With .Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 10092543
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End With
            Sheet1.Protect
        End If
    End Sub
    The line at the top of the module is the most important.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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