简体   繁体   中英

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

I should mention that I open the workbook through the Windows Task Scheduler. 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.

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.

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.

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. Try to paste this code to worksheet which contains "start_on_open" range:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM