Can't make VBA act on MsgBox results

alisser

New Member
Joined
Jan 10, 2009
Messages
12
Help again: I'm trying to allow a user to decide not to delete a row in a protected table (part of a macro) if they click Cancel in a msgbox that pops up. I was able get the msgbox to pop up when the RemoveRows sub is run, but can't make it exit the RemoveRows sub if the user clicks Cancel.
Here is a private sub with msg, and the sub to delete:

Private Sub cmdMessageBoxDelete_Click()
MsgBox "Are you sure you want to delete the bottom row?" & _
vbCrLf & "You can't undo after clicking OK. ", _
VbMsgBoxStyle.vbOKCancel
End Sub

Sub RemoveRows()
Dim oLst As ListObject
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="myPW"
If ActiveSheet.ListObjects.Count > 0 Then
For Each oLst In ActiveSheet.ListObjects
If oLst.ListRows.Count > 1 Then
' Give user a way out of deleting row
Call cmdMessageBoxDelete_Click ' From private sub in this module
oLst.ListRows(oLst.ListRows.Count).Delete
End If
Next oLst
End If
ActiveSheet.Protect AllowSorting:=True, Password:="myPW"
End Sub

Help appreciated.
Thanks
-Alisser
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try:
Code:
Private Sub cmdMessageBoxDelete_Click()
if MsgBox ("Are you sure you want to delete the bottom row?" & vbCrLf & "You can't undo after clicking OK. ", VbMsgBoxStyle.vbOKCancel) = vbcancel then exit sub
End Sub
 
Sub RemoveRows()
Dim oLst As ListObject
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="myPW"
If ActiveSheet.ListObjects.Count > 0 Then
    For Each oLst In ActiveSheet.ListObjects
        If oLst.ListRows.Count > 1 Then
        ' Give user a way out of deleting row        
        Call cmdMessageBoxDelete_Click ' From private sub in this module
oLst.ListRows(oLst.ListRows.Count).Delete
        End If
    Next oLst
End If
ActiveSheet.Protect AllowSorting:=True, Password:="myPW"
End Sub
 
Upvote 0
Greetings Alisser,

Are you wanting to exit the sub outright, or just skip the delete in that particular cycle of the loop?

If wanting to bail outright, maybe skip calling another procedure and toss a line in there. Not tested but something akin to:
Rich (BB code):
Sub RemoveRows()
Dim oLst As ListObject
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="myPW"
    If ActiveSheet.ListObjects.Count > 0 Then
        For Each oLst In ActiveSheet.ListObjects
            If oLst.ListRows.Count > 1 Then
                If Not MsgBox("Are you sure?", vbYesNo Or vbDefaultButton2, "Read Me!") = vbYes Then
                    GoTo JumpOut
                End If
            oLst.ListRows(oLst.ListRows.Count).Delete
            End If
        Next oLst
    End If
        
JumpOut: ActiveSheet.Protect AllowSorting:=True, Password:="myPW"
End Sub
Mark
 
Upvote 0
Try a Function

Something like

Code:
Private Function cmdMessageBoxDelete_Click()
    cmdMessageBoxDelete_Click = MsgBox("Are you sure you want to delete the bottom row?" & _
    vbCrLf & "You can't undo after clicking OK. ", VbMsgBoxStyle.vbOKCancel)
End Function

Sub aTest()
    Dim answer As Long
    
    answer = cmdMessageBoxDelete_Click
    If answer <> 1 Then Exit Sub
    MsgBox "Do Stuff"
End Sub

M.
 
Upvote 0
Thanks - I tries that code, but the sub is still not reacting to the msgbox. When I click OK, it deletes a row correctly, but if I click cancel, it still deletes a row. ??
 
Upvote 0
Well, I am glad you got it working. Cat-killin' curiousity... What did it?
 
Upvote 0
Oh, it was putting is all in one as you suggested - copied your code exactly. That is all I really needed and the shorter message was better anyway.
Thanks GTO!
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top