PDA

View Full Version : Solved: Messagebox in macro



Rejje
11-19-2010, 03:34 PM
Hi!

Total vba noob need lots of :help! How do I insert a message box in the code below (macro in a button) that askes for a confirmation to either quit or prosecute?

I want the message box to say "Do you really want to quit without saving?" Then choises are buttons "Yes" or "No" in the message box..


Sub CloseNoSave()
'Close the workbook without saving it
ThisWorkbook.Close savechanges:=False
End Sub

Maybe you find a better way than to modify this code :dunno

Simon Lloyd
11-19-2010, 03:56 PM
Moved to appropriate forum.

I guess you mean if the workbook has already been saved? because that feature is built in to excel!
For already saved workbook, code goes in the Thisworkbook code module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Saved = True Then
ans = MsgBox("Do you really want to quit without saving", vbYesNoCancel, "Confirm Action")
Select Case ans
Case Is = vbYes
Exit Sub
Case Is = vbNo
Cancel = True
Case Is = vbCancel
Cancel = True
End Select
End If
End Sub

Rejje
11-19-2010, 05:24 PM
Moved to appropriate forum.

I guess you mean if the workbook has already been saved? because that feature is built in to excel!
For already saved workbook, code goes in the Thisworkbook code module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Saved = True Then
ans = MsgBox("Do you really want to quit without saving", vbYesNoCancel, "Confirm Action")
Select Case ans
Case Is = vbYes
Exit Sub
Case Is = vbNo
Cancel = True
Case Is = vbCancel
Cancel = True
End Select
End If
End Sub


Thanks! Beautiful!