How to get out of BeforeClose event without closing?

  • Thread starter Thread starter 42N83W
  • Start date Start date
4

42N83W

Excel 2002
Windows XP Pro SP 2

I am trying to simulate one of those "Do you really, REALLY want to close
this workbook" messages when a user tries to close a workbook. I am using a
message box with the vbYesNo constant and I can successfully determine if
Yes or No was clicked. If the user selects "No", which in my case would be
to NOT close the workbook, how do I get out of the BeforeClose event without
closing?

All help appreciated.

Thanks!

-gk-


=================================================
The creative act is not the province of remote oracles or rarefied geniuses
but a transparent process that is open to everyone.
-Greg Kot in Wilco Learning How To Die-
 
Hi gk

like this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("yes or no", vbYesNo) = vbNo Then
Cancel = True
End If
End Sub

Cheers
JulieD
 
Hi

As first line in the event use Cancel = true

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim answer
Cancel = True
answer = MsgBox("do you want to close", vbYesNo, "something")
If answer = vbOK Then
ThisWorkbook.Close
Else
'do nothing
End If
End Sub
 
Oops, Why don't i test first<g>

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim answer
answer = MsgBox("do you want to close", vbYesNo, "something")
If answer = vbYes Then
'close
Else
Cancel = True
End If
End Sub
 
Thank You both Julie and Ron.

It didn't even occur to me to look at the argument of the BeforeClose event,
and I should know better! Setting Canel = True did the trick.

Thanks again!

-gk-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top