PDA

View Full Version : [SOLVED] Before_Close Sub



av8tordude
02-05-2014, 08:46 AM
I have this code that checks if other workbooks are open. I'm tried many times to reduce the redundant code. Is there a simpler or better way to write this code?


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Workbooks.Count > 1 Then
If MsgBox("You have other workbooks open. Any changes made in the other workbooks will be loss. Do you want to continue?", vbYesNo + vbQuestion) = vbYes Then
'ThisWorkbook.Unprotect Password:="T0nyul!a"
TitleBarOpen
Application.ScreenUpdating = 0
HideAll
'ThisWorkbook.Protect Password:="T0nyul!a"
ThisWorkbook.Save
Application.Visible = False
frmLogo.Show
Application.Visible = True
Application.Quit
Else
Cancel = True
End If
Else
'ThisWorkbook.Unprotect Password:="T0nyul!a"
TitleBarOpen
Application.ScreenUpdating = 0
HideAll
'ThisWorkbook.Protect Password:="T0nyul!a"
ThisWorkbook.Save
Application.Visible = False
frmLogo.Show
Application.Visible = True
Application.Quit
End If
End Sub

Aflatoon
02-05-2014, 08:55 AM
You mean like:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Workbooks.Count > 1 Then
If MsgBox("You have other workbooks open. Any changes made in the other workbooks will be loss. Do you want to continue?", vbYesNo + vbQuestion) = vbYes Then
Call foo
Else
Cancel = True
End If
Else
Call foo
End If
End Sub

Sub foo()
'ThisWorkbook.Unprotect Password:="T0nyul!a"
TitleBarOpen
Application.ScreenUpdating = 0
HideAll
'ThisWorkbook.Protect Password:="T0nyul!a"
ThisWorkbook.Save
Application.Visible = False
frmLogo.Show
Application.Visible = True
Application.Quit

End Sub


?

snb
02-05-2014, 08:59 AM
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Workbooks.Count > 1 then
if MsgBox("You have other workbooks open. Any changes made in the other workbooks will be lost. Do you want to continue?", vbYesNo + vbQuestion) = vbNo then
Cancel = True
exit sub
end if
end if

'ThisWorkbook.Unprotect Password:="T0nyul!a"
TitleBarOpen
Application.ScreenUpdating = 0
HideAll
'ThisWorkbook.Protect Password:="T0nyul!a"
ThisWorkbook.Save
Application.Visible = False
frmLogo.Show
Application.Visible = True
Application.Quit
End Sub

av8tordude
02-05-2014, 09:02 AM
Thank you guys. :-)