PDA

View Full Version : Excel file close



trevb
03-02-2010, 12:42 AM
Hi, can someone help please.....
I have entered the following code but for some reason when the No button is clicked nothing happens and I have to click it a second time to close the file, the Yes button works fine ?
Am I missing something really obvious ?
Any help much appreciated.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Ans As String
Ans = MsgBox("Do you want to save the workbook?", vbYesNo)
Select Case Ans
Case vbYes
ThisWorkbook.Save
Case vbNo
ActiveWorkbook.Close False
End Select
End Sub

Regards T

Bob Phillips
03-02-2010, 02:08 AM
What do you want to happen, your code says don't close it.

trevb
03-02-2010, 02:17 AM
On vbYes save and close and vbNo close the workbook

Bob Phillips
03-02-2010, 02:30 AM
Just remove


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Ans As String
Ans = MsgBox("Do you want to save the workbook?", vbYesNo)
Select Case Ans
Case vbYes
ThisWorkbook.Save
Case vbNo
ThisWorkbook.Saved=True
End Select
End Sub

trevb
03-02-2010, 03:20 AM
Many thanks xld works fine.