Hi,
I have a problem with some of my code which has baffled me for ages & was hoping someone can help me resolve the issue.
I have a BeforeSave macro that works ok when you save manually, however there are 2 other instances that do not work:
1) Using "ThisWorkbook.Save" in another macro does not save the changes. (when you close the workbook and answer no to saving changes you lose changes that were made before running the "ThisWorkbook.Save")
2) If i have a 2nd workbook open at the same time I get stuck in an eternal loop when asked if i want to save changes when closing the workbook. (just keep getting the dialog box asking if i want to save changes, click yes and it comes back)
I hope I have explained this clearly and really do appreciate any advice that can be given
BeforeSave Code:
Regards![]()
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Application.EnableEvents = False Application.ScreenUpdating = False '************************************************************************************************ If SaveAsUI = True Then ' If user tries to SaveAs then cancel Cancel = True MsgBox "You cannot use 'Save As' with this workbook. Use 'Save'", vbCritical, "Cannot Save As" '************************************************************************************************ Else ' Save workbook in opening workbook state ' Cancel original save request Cancel = True ' Unprotect worksheets Call UnProtect_Worksheets ' Unprotect workbook Call UnProtect_Workbook ' Get current worksheet name Dim CurrentSheet As String CurrentSheet = ActiveSheet.Name ' Select Open Message worksheet & hide all other worksheets Dim s As Worksheet Const wsKeep As String = "Open Message" Worksheets(wsKeep).Visible = True Worksheets(wsKeep).Select For Each s In Worksheets If s.Name <> wsKeep Then s.Visible = xlSheetHidden Next s ' Make Enable Macro Instructions worksheet visible Sheets("Enable Macro Instructions").Visible = True ' Protect worksheets Call Protect_All_Worksheets ' Protect workbook Call Protect_Workbook ' Save this workbook ThisWorkbook.Save '************************************************************************************************ ' Return to worksheet that was open before save ' Unprotect worksheets Call UnProtect_Worksheets ' Unprotect workbook Call UnProtect_Workbook ' Select worksheet that was open when save was selected by user Sheets("" & CurrentSheet & "").Visible = True Worksheets("" & CurrentSheet & "").Select ' Hide Enable Macro Instructions & Open Message worksheets Sheets("Enable Macro Instructions").Visible = False Sheets("Open Message").Visible = False ' Protect worksheets Call Protect_All_Worksheets ' Protect workbook Call Protect_Workbook '************************************************************************************************ End If Application.ScreenUpdating = True Application.EnableEvents = True End Sub
Steve
Bookmarks