Macro Enabled Excel文件上的自动保存文件错误

时间:2017-03-01 04:19:07

标签: excel vba excel-vba runtime-error autosave

我正在使用Macro Enabled文件(二进制表),它有许多模块/表单,有时当我的笔记本电脑出现问题并且excel突然关闭时,我的自动保存文件不起作用。

我在每个自动保存的文件上都出现了错误:

  

运行时错误' 9'

     

下标超出范围

我的自动保存频率是5分钟,以保存我的背部,但有趣的是,这个文件不起作用。

我甚至无法关注错误的位置,因为唯一打开的是自动保存的文件是白色空白页。 (这就是为什么关于SO的其他运行时错误9个问题没有回答我的问题)原因是什么,可能的解决方案是什么?

更新:我在该工作簿中的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

1 个答案:

答案 0 :(得分:0)

在与很多博客讨论后,我终于找到了两个解决方案。 (第一个就像错误处理一样,第二个就是解决问题。)特别感谢YowE3k,jkpieterse和Ryan Wells。

首先,我想提一下原因,为什么会这样:

  

代码失败,因为当Excel恢复文件时,它会添加一些文本   到窗口的标题,以便“FileName.xlsx”成为   类似“FileName.xlsx [用户最后保存的版本]”(jkpieterse)。

解决方案:

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

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

ActiveWindow.Visible = False

Windows(ThisWorkbook.Name).Visible = True

行,这将阻止问题。

2)原始解决方案(jkpieterse)

ThisWorkbook对象表中使用以下例程。

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)

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