我有一个相当大的工作簿,有一个非常复杂的VBA项目。
该工作簿还有一个自定义功能区......
在Workbook_Open事件中也发生了很多事情,取消保护和重新保护(userInterfaceOnly),隐藏和显示各种工作表,存储对功能区的引用等。
如果未自动启用宏,则会打开工作簿,并显示启用按钮,当我单击它时,一切都按预期发生。
当文件受信任且宏自动运行时,会出现问题。在这种情况下,它会使应用程序崩溃。
就好像,如果用户必须单击按钮暂停,它会完成所有Excel准备工作,显示功能区,然后执行我的所有Workbook_Open工作,但如果没有启用按钮,那么Excel自己的启动东西和显示功能区似乎与我的Workbook_Open事件纠缠在一起,导致它崩溃。
这发生在2007年,2010年和2013年以及Windows 7和Windows 7上。 8(0.1)
我怀疑它与功能区有关,因为有时问题不是完全崩溃,而是工作簿打开,功能区区域只是空白。
仅供参考,Workbook_Open事件中的代码如下所示:
enter code here
Private Sub Workbook_Open()
Dim newHour As Single
Dim newMinute As Single
Dim newSecond As Single
Dim waitTime As Date
Application.ScreenUpdating = False
' To activate the ribbon in v2007
Application.SendKeys "%Q{RETURN}"
Call ResetTheSheets
Splash.Visible = xlSheetVisible
Splash.Protect shtHidden.Range("iWord")
Call ShowHideSplash
ExecSumm.Visible = xlSheetVisible
GetStarted.Visible = xlSheetVisible
' Look at the properties in the Prop sheet to see what should be displayed
If Prop.Range("PropHideGetStarted") Then
On Error Resume Next
With ExecSumm
.Activate
'Range("C4").Select
.Unprotect Password:=shtHidden.[iWord]
.Visible = xlSheetVisible
.Range("A1:A" & .Range("LastRowCol").Row).EntireRow.Hidden = False
.Range(.Cells(1, 1), .Cells(1, .Range("LastRowCol").Column)).EntireColumn.Hidden = False
' Check to see if the template column has anything in
If WorksheetFunction.CountA(.Range("TemplateCol")) = 0 Then .Range("TemplateCol").EntireColumn.Hidden = True
' Check to see if the prior year should be hidden
If Not Prop.Range("PropBFbalances") Then .Range("ppTitle").EntireColumn.Hidden = True
.Activate
End With
Else
GetStarted.Select
End If
ActiveWindow.ScrollRow = 1
FormShow:
Err.Clear
On Error GoTo FormShow
frmSplash.Show msoFalse
shtHidden.Range("iBalance") = "TRUE"
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'' Schedule the start to avoid 'startup-crashes' - this didn't work as a way to prevent
' newHour = Hour(Now())
' newMinute = Minute(Now())
' newSecond = Second(Now()) + 1
' waitTime = TimeSerial(newHour, newMinute, newSecond)
' Application.Wait waitTime
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Err.Clear
On Error GoTo 0
Application.OnTime Now + TimeValue("00:00:01"), "StartUpMacro"
End Sub
答案 0 :(得分:0)
您可以尝试:
一个。让excel按照你的需要等待预定时间在sendkey之前或之后做好准备:
Application.Wait(Now + #0:00:05#) '5 sec for example
礼貌:link
湾使用msgbox弹出(类似于宏启用按钮)以禁止快速执行其余代码。