Vba 启用宏的Excel文件出现自动保存文件错误
我使用的是启用宏的文件(二进制表),它有许多模块/表单,有时当我的笔记本电脑出现问题,excel突然关闭时,我的自动保存文件无法工作 我在每个自动保存的文件上都会出现该错误: 运行时错误“9” 下标超出范围 我的自动保存频率是5分钟来保存我的背部,但有趣的是,这不适用于此文件 我甚至无法理解错误在哪里,因为在自动保存的文件上打开的唯一内容是白色空白页。(这就是为什么其他运行时错误9个问题没有回答我的问题)原因是什么,可能的解决方案是什么 更新:工作簿\u打开我在该工作簿中的事件Vba 启用宏的Excel文件出现自动保存文件错误,vba,excel,runtime-error,autosave,Vba,Excel,Runtime Error,Autosave,我使用的是启用宏的文件(二进制表),它有许多模块/表单,有时当我的笔记本电脑出现问题,excel突然关闭时,我的自动保存文件无法工作 我在每个自动保存的文件上都会出现该错误: 运行时错误“9” 下标超出范围 我的自动保存频率是5分钟来保存我的背部,但有趣的是,这不适用于此文件 我甚至无法理解错误在哪里,因为在自动保存的文件上打开的唯一内容是白色空白页。(这就是为什么其他运行时错误9个问题没有回答我的问题)原因是什么,可能的解决方案是什么 更新:工作簿\u打开我在该工作簿中的事件 Private
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 首先,我想谈谈发生的原因: 该代码失败,因为Excel恢复文件时会添加一些文本 添加到窗口的标题,使“FileName.xlsx”变为 类似于“FileName.xlsx[用户上次保存的版本]”(jkpieterse) 解决方案: 1) 基本错误处理(Ryan Wells) 如果我们知道哪一行导致错误,我们可以注释掉该行以保护工作簿。因此,如果您注释掉(或简单删除)以下内容: 线路,这将停止问题 2) 原始溶液(jkpieterse) 在
本工作簿
对象工作表中使用以下例程
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
然后,在工作簿\u Open
事件中
而不是Windows(thishworkbook.Name)。Visible=True
使用ShowaWindow(thishworkbook.Name)
然后它会像一个符咒一样工作 您可以显示您的vba自动保存代码吗?可能是您应该首先在每个子/函数中添加文本文件日志写入,以创建有关出错位置的轨迹。很痛苦,但我找到了一个导致Excel崩溃的错误。然后在崩溃的子函数/Function.0m3r中添加更多日志或设置断点。我的文件中没有任何自动保存代码,我说的是Excel自己的自动恢复系统。这一个可以很好地处理所有其他文件(当任何事情发生时),但不适用于这一个。帕特里克,谢谢你的解释,但老实说,我不知道如何创建这些轨迹来跟踪哪里出了问题。问题是我有很多模块和表单,它们不确定在哪里找到这些断点。我需要等待下一个excel崩溃(导致自动恢复)来跟踪这件事,我们不知道什么时候会再次发生。请显示您的BeforeSave或AfterSave函数,也许excel autosave功能在其中一个函数上有问题。如果您在打开文件时遇到错误,您是否有任何工作簿\u打开事件?
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