启用宏 Excel 文件的自动保存文件错误

Autosaved file error on Macro Enabled Excel file

我正在使用启用宏的文件(二进制 sheet),其中有许多 modules/forms,有时当我的笔记本电脑出现问题并且 excel 突然关闭时,我的自动保存文件不会没用。

我在每个自动保存的文件上都遇到了这个错误:

Run-time error '9'

Subscript out of range

我的自动保存频率是 5 分钟以节省时间,但有趣的是,这对这个文件不起作用。

我什至无法找出错误所在,因为在该自动保存文件上打开的唯一内容是白色空白页。 (这就是为什么其他 运行-time error 9 问题在 SO 上没有回答我的问题)什么样的事情会是原因,可能的解决方案是什么?

更新:Workbook_Open 我在该工作簿中的事件

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

这是我的 SplashUserForm 的内容:

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

在和很多博客的很多人讨论后,最后我想出了两个解决方案。 (第一个类似于错误处理,第二个完全解决了问题。)特别感谢 YowE3k、jkpieterse 和 Ryan Wells。

首先,我想提一下发生的原因:

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).

解决方案:

1) 基本错误处理 (Ryan Wells)

如果我们知道哪一行导致错误,我们可以注释掉该行以保护我们的工作簿。因此,如果您注释掉(或简单地删除):

ActiveWindow.Visible = False

Windows(ThisWorkbook.Name).Visible = True

行,这将解决问题。

2) 原始解决方案 (jkpieterse)

在您的 ThisWorkbook 对象中使用以下例程 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

然后,在 Workbook_Open 事件中,

而不是Windows(ThisWorkbook.Name).Visible = True

使用ShowaWindow(ThisWorkbook.Name)

然后它会像一个魅力一样工作!