Excel Code Blocking other Excel Sheets from opening

不羁岁月 提交于 2020-01-17 06:25:03

问题


My Excel file blocks other Excels files from opening.

I have the below code

Public Sub Workbook_Open()
    Application.OnTime Now + TimeValue("00:00:03"), "CommonWorkBook.prepareSheets"
End Sub

In PrepareSheets Macro, I take data from a txt file every 60 seconds.

The code is as follows

Do While 1 = 1
    ' Do Something
    pause 60
 Loop

After opening my .xlsm file, I am unable to open other .xlsm files.

If I close my .xlsm file, other files gets opened.


回答1:


The problem is that when Pause is running it is still running. If you look in the task manager while pause 60 is being executed then you will see that it is using up almost all CPU from one of your cores:

Even if you could fix it -- do you really want to hog so much of your system's resources?

VBA is old technology -- only minor changes since the late 1990s. It is single-threaded. Only one single-threaded instance of the interpreter for each instance of the Excel application. The running code is what is preventing another macro-enabled workbook from opening.

There really is no easy way to do multithreading in Excel (but see this question). You should look instead for a solution involving Application.OnTime. Don't pause for 60 seconds -- schedule for 60 seconds in the future. See this (as suggested by @Ralph) for more details.



来源:https://stackoverflow.com/questions/34414525/excel-code-blocking-other-excel-sheets-from-opening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!