问题
I am using Macro Enabled file (binary sheet) which has many modules/forms and sometimes when something go wrong with my laptop and excel shuts down suddenly, my autosave files doesn't work.
I get that error on each autosaved file:
Run-time error '9'
Subscript out of range
My autosave frequency is 5 minutes to save my back but that interestingly doesn't work for this file.
I am even not able to follow where the error is because the only thing is opening on that autosaved file is White blank page. (That's why other Run-time error 9 questions on SO were not answer of my question) What kind of thing would be the reason and what is the possible solution?
UPDATE: Workbook_Open Events that I have in that Workbook
Private Sub Workbook_Open()
Application.ScreenUpdating = False
ActiveWindow.Visible = False
SplashUserForm.Show
Windows(ThisWorkbook.Name).Visible = True
Application.ScreenUpdating = True
With Sheet5
.Unprotect Password:=""
.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True, UserInterfaceOnly:=True
.EnableOutlining = True
End With
With Sheet16
.Unprotect Password:=""
.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True, UserInterfaceOnly:=True
.EnableOutlining = True
End With
End Sub
And here what my SplashUserForm has:
Private Sub UserForm_Activate()
Application.Wait (Now + TimeValue("00:00:01"))
SplashUserForm.Label1.Caption = "Loading Data..."
SplashUserForm.Repaint
Application.Wait (Now + TimeValue("00:00:01"))
SplashUserForm.Label1.Caption = "Creating Forms..."
SplashUserForm.Repaint
Application.Wait (Now + TimeValue("00:00:01"))
SplashUserForm.Label1.Caption = "Opening..."
SplashUserForm.Repaint
Application.Wait (Now + TimeValue("00:00:01"))
Unload SplashUserForm
End Sub
Private Sub UserForm_Initialize()
HideTitleBar Me
With Me
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
End With
End Sub
Option Explicit
Option Private Module
Public Const GWL_STYLE = -16
Public Const WS_CAPTION = &HC00000
Public Declare Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function DrawMenuBar _
Lib "user32" ( _
ByVal hWnd As Long) As Long
Public Declare Function FindWindowA _
Lib "user32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Sub HideTitleBar(frm As Object)
Dim lngWindow As Long
Dim lFrmHdl As Long
lFrmHdl = FindWindowA(vbNullString, frm.Caption)
lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
lngWindow = lngWindow And (Not WS_CAPTION)
Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
Call DrawMenuBar(lFrmHdl)
End Sub
回答1:
After discussing with many people from many blogs, finally I came up with two solution. (First one is like error handling and the second one exactly solves the issue.) Special thanks to YowE3k,jkpieterse and Ryan Wells.
First of everything, I would like to mention the reason, why it was happening:
The code fails because when Excel recovers a file, it adds some text to the caption of the window, so that "FileName.xlsx" becomes something like "FileName.xlsx [Version last saved by user]" (jkpieterse).
Solutions:
1) Basic Error Handling (Ryan Wells)
If we know which line is causing error, we can comment out that line to protect our workbook. So if you comment out (or simply delete) the :
ActiveWindow.Visible = False
Windows(ThisWorkbook.Name).Visible = True
lines, that will stop the problem.
2) Original Solution (jkpieterse)
use a below routine in your ThisWorkbook
object sheet.
Sub ShowaWindow(sFileName As String)
Dim oWb as Workbook
For Each oWb In Workbooks
If lCase(owb.Name) = lCase(sFileName) Then
oWb.Windows(1).Visible = True
Exit For
End If
Next
End Sub
Then, in the Workbook_Open
event,
Instead of Windows(ThisWorkbook.Name).Visible = True
UseShowaWindow(ThisWorkbook.Name)
Then it will work like a charm!
来源:https://stackoverflow.com/questions/42523109/autosaved-file-error-on-macro-enabled-excel-file