msg box

  • Thread starter Thread starter gatarossi
  • Start date Start date
G

gatarossi

dear all,

I have this code, but if I click in cancel, the worbook closes, and if
I click in yes or in no, I need to click twice to complete the
operation, because after the first click the msgbox apears again. This
code is correct or I'm getting crazy?

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim iselection As Integer

iselection = MsgBox("Deseja salvar as alterações realizadas?",
vbYesNoCancel + vbQuestion)

Select Case iselection
Case Is = vbYes
Sheets("acesso").Visible = True
Sheets("Evolução de Horas").Visible = xlVeryHidden
Sheets("aux").Range("d2:d9").ClearContents
ThisWorkbook.Close SaveChanges:=True
Case Is = vbNo
Sheets("aux").Range("d2:d9").ClearContents
ActiveWorkbook.Close SaveChanges:=False
End Select

End Sub

Thanks!

André.
 
Each .close will cause the event to fire again. You can use
application.enableevents to stop the second execution.

And I changed activeworkbook to me. I figured you meant the workbook that owned
the code--not always the activeworkbook.



Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim iselection As Integer

iselection = MsgBox("Deseja salvar as alterações realizadas?",
vbYesNoCancel + vbQuestion)

application.enableevents = false
Select Case iselection
Case Is = vbYes
Sheets("acesso").Visible = True
Sheets("Evolução de Horas").Visible = xlVeryHidden
Sheets("aux").Range("d2:d9").ClearContents
me.Close SaveChanges:=True
Case Is = vbNo
Sheets("aux").Range("d2:d9").ClearContents
me.Close SaveChanges:=False
End Select
application.enableevents = true
End Sub
 
Back
Top