I have recently finished an Excel workbook that utilizes a fair amount of VBA. The spreadsheet is always able to perform all of its intended functions, and there are no issues when it is the only workbook open in Excel. However, when any other workbook is open (unrelated files included), the workbook will not close. Clicking on the red "x" in the top right corner simply does not do anything, and no error messages are shown. In order to close it, all other Excel windows must be closed beforehand.
While this does not detract from the functionality of the workbook itself, enough people will be using it that I want to avoid bringing about the inconvenience of having to close any other workbooks they have open before being able to close this one.
So, does anyone have any ideas as to what is causing this to happen? I do have code for the Workbook_Close() event, but this is designated as private and only located within the ThisWorkbook part of this spreadsheet. I'm confused as to why another sheet with no VBA involved prevents this one from closing. Please let me know if there is anything I can clarify, thank you.
EDIT - Here is the code for the Workbook_BeforeClose() event:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Me.Saved = False Then Me.Save
Call PrepareWorkbook
End Sub
Sub PrepareWorkbook()
Application.ScreenUpdating = False
Call UnprotectWorkbook
Dim masterSheet As Worksheet
Set masterSheet = ActiveWorkbook.Worksheets("PLANNING")
For Each sheet In ActiveWorkbook.Worksheets
If Not (sheet.Name = "README" Or sheet.Name = "PLANNING" Or sheet.Name = "PREV") Then
sheet.Visible = xlSheetVeryHidden
End If
If (sheet.Name = "README" Or sheet.Name = "PLANNING" Or sheet.Name = "PREV" Or sheet.Name = "ADMIN") Then
Call ProtectSheet(ActiveWorkbook.Worksheets(sheet.Name))
Else
Call ProtectPlannerSheet(ActiveWorkbook.Worksheets(sheet.Name))
End If
Next
If ActiveWorkbook.ActiveSheet.Name <> "PLANNING" Then
masterSheet.Select
End If
Call ProtectWorkbook
Application.ScreenUpdating = True
End Sub
I should note that commenting out the call to PrepareWorkbook() within the Workbook_BeforeClose() event did not solve the problem.
Workbook_BeforeClose
event? – RoryWorkbook_Close
event in Excel. I assumed that was a typo on your part. The code you have shouldn't ever run unless it's called by some other code. – Rory