[英]Excel does not fully open when starting macro on Workbook_Open
I wrote a makro that I want to run directly when the workbook is opened.我写了一个makro,我想在打开工作簿时直接运行。 It works, however, the macro starts before the real spreadsheet opens completely, and I only see the Excel splash the entire time the makro runs.它可以工作,但是,宏在真正的电子表格完全打开之前就开始了,我只看到 Excel 在 makro 运行的整个过程中飞溅。
I should mention that I open the workbook through the Windows Task Scheduler.我应该提到我通过 Windows 任务计划程序打开工作簿。 Once the file is opened, the macro go through a loop and open a list of other Excel files that are updating external data using the SAP Analysis plugin.打开文件后,宏 go 通过循环打开其他 Excel 文件的列表,这些文件正在使用 SAP Analysis 插件更新外部数据。
I call the spreadsheet with the macro as a scheduled task to update data and then close it again automatically:我将带有宏的电子表格作为计划任务调用以更新数据,然后再次自动关闭它:
Private Sub Workbook_Open()
If Range("start_on_open").value = "YES" Then
Call Process_action_table
End If
End Sub
start_open
is a named range in my sheet, and when I set it to "YES", the makro will start automatically. start_open
是我工作表中的一个命名范围,当我将其设置为“YES”时,makro 将自动启动。
While the function "Process_action_table" is running, I display status information in the spreadsheet, but of course with only the splash screen visible, nothing is really displayed.当 function“Process_action_table”正在运行时,我在电子表格中显示状态信息,但当然只有启动屏幕可见,实际上没有显示任何内容。
When I run the macro "manually", everything works fine.当我“手动”运行宏时,一切正常。 It seems to me that it is somehow as if there was not enough processing time left to open the spreadsheet properly.在我看来,好像没有足够的处理时间来正确打开电子表格。
I have added the following code at the start, but to no avail:我在开始时添加了以下代码,但无济于事:
t_end = Now + TimeValue("0:0:10")
Do While Now < t_end
DoEvents
Loop
Any suggestions would be greatly appreciated.任何建议将不胜感激。
divingTobi.潜水托比。 If worksheet loading is indispensable condition instead of using Workbook_Open()
utilization of Worksheet_Activate()
is suitable.如果工作表加载是必不可少的条件,而不是使用Workbook_Open()
,则使用Worksheet_Activate()
是合适的。 Try to paste this code to worksheet which contains "start_on_open" range:尝试将此代码粘贴到包含“start_on_open”范围的工作表中:
Private Sub Worksheet_Activate()
If Range("start_on_open").value = "YES" Then
Call Process_action_table
End If
End Sub
I have not tried this code.我没有尝试过这段代码。 Please give feedback if it is good or not.请反馈好不好。 It will loop/wait for workbook to be writeable.它将循环/等待工作簿可写。
Private Sub Workbook_Open()
Dim wb As Workbook
Set wb = ThisWorkbook
If Range("start_on_open").value = "YES" Then
'wait for workbook to writeable
Do Until wb.ReadOnly = False
Application.Wait Now + TimeValue("00:00:01")
Loop
Call Process_action_table
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.