I have a complex workbook with quite some code, and used by many users spread over different places... and I am not a professional programmer.
On the Workbook_Open event, I deal with a splash screen, password protecting and showing the main sheets and setting some global variables.
This file has been working reasonably stably for some months, but more recently I began working with the Ribbon to improve the user-interface (the goal is to replace a floating form that serves as a menu). It then became unstable, often crashing Excel on opening the file. Overall, the situation is very similar to this: Excel [VBA] crashes on Workbook Open when activating a sheet.
I had already removed (or commented) all code related to Ribbon with no success, and the Wait command suggested on the thread above didn't help either.
If the workbook is opened in protected mode and then the code is enabled, it runs smoothly. More interesting, if I close it without saving, it will run well next time I open it, without the need of the protected mode. But as soon as I save the file, it will crash next time it is opened.
In fact, the crash is not immediate; it just seems that the code is in an infinite loop. If I go to the VBAProject window, the title bar shows "[running...]" (or something alike - my installation is in French...), and if the ThisWorkbook module was opened before, we cannot see the code, but rather some frozen image of some part of the screen. When I try to interrupt execution, then it really crashes.
I already exported, deleted and re-imported all code and forms - no luck. The code is usually password protected, but I removed it for testing. Now I have no clue what to do. Any ideas, please?
The code of the Open event is:
Dim S As Object, rng As Range
'On Error GoTo Terminate
Application.Wait Now + #12:00:05 AM#
Application.ScreenUpdating = False
Application.EnableEvents = False
NSheets = Me.Sheets.Count
For Each S In Me.Sheets
Set rng = AuxTables.Range("AT_SheetNames").Find(S.CodeName, , xlValues, xlWhole)
If Not rng Is Nothing Then
'Set sheet visibility
If S.CodeName <> "Warning" Then S.Visible = Val(rng.Offset(0, 16))
'Ensure all standard tabs are protected
S.Protect Password:="xxxx", UserInterfaceOnly:=True, DrawingObjects:=False, _
Contents:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True
S.EnableOutlining = True
'Erase marker of Degraded Mode
S.Range(IIf(S.CodeName <> "CashCurves", "C", "B") & "1").ClearContents
Call SetSheetsDefaults(S)
End If
Next S
Warning.Visible = xlHidden
Call EnableRecalculate(True)
bl_UpdateSupplySummary = True
Application.EnableEvents = True
Application.ScreenUpdating = True
On Error Resume Next
AppActivate Application.Caption, True
Cover.Activate
Exit Sub
Terminate:
Dim Msg As String
Msg = "Description: " & Err.Description & Chr(10)
Msg = Msg & "Module: Workbook" & Chr(10)
Msg = Msg & "Procedure: Open" & Chr(10) & Chr(10)
Msg = Msg & "Execution will be interrupted." & Chr(10) & Chr(10)
Msg = Msg & "Please print this screen and send it to [email protected]"
MsgBox Msg, vbCritical, "Unexpected Error " & Err.Number
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Thanks.
Stopto the top of the code and then try to debug it (if it works, try movingStoplower until you isolate the offending code). - martinApplication.Waitand the two following lines. No changes. - Edgard