简体   繁体   English

打开其他 Excel 文件时宏速度变慢

[英]Macro slow when other Excel files are open

I've seen other users post somewhat the same question, but the core problem doesn't seem to be the same (as far as I can tell).我看到其他用户发布了一些相同的问题,但核心问题似乎并不相同(据我所知)。

I have an Excel workbook that goes through about 80 000 rows and four columns of data.我有一个 Excel 工作簿,其中包含大约 80 000 行和四列数据。 This takes about 1-2 seconds at the most.这最多需要大约 1-2 秒。 The workbook does do operations in other worksheets but for the test I have turned those subs off.该工作簿确实在其他工作表中执行操作,但为了测试,我已关闭这些子程序。 If I open an .xlsx of about 10 Mb it takes a bit longer but not much.如果我打开一个大约 10 Mb 的 .xlsx,它需要更长的时间,但不会太多。 If I open up an .xlsm with some considerable code, it takes about 6-7 seconds to do the same thing.如果我用一些相当多的代码打开一个 .xlsm,做同样的事情大约需要 6-7 秒。

What I have read so far is that one should use set variables of ranges instead of cell references, calculation set to manual, screenupdating off.到目前为止我读到的是,应该使用范围的设置变量而不是单元格引用,将计算设置为手动,关闭屏幕更新。 None of these seem to do the trick though.不过,这些似乎都不起作用。

If the events were not disabled, this could also be added just before the code begins:如果事件没有被禁用,这也可以在代码开始之前添加:

Application.EnableEvents = False

Changing it to True when everything is finished.一切完成后将其更改为 True。

If you wish to optimize things further, one possibility is to assign it to a variable:如果您希望进一步优化事物,一种可能性是将其分配给一个变量:

Dim Temp as Variant
Temp = ActiveSheet.UsedRange.Value

Analyze and change Temp, and then put it back分析和改变Temp,然后把它放回去

ActiveSheet.UsedRange = Temp

I finally figured it out.我终于弄明白了。 It has to do with instances of Excel.它与 Excel 的实例有关。 If multiple heavy macro files are opened in the same instance of Excel it takes a lot longer than if one instance per file is used.如果在同一个 Excel 实例中打开多个繁重的宏文件,则比每个文件使用一个实例需要更长的时间。 I don't know why this is, but from what I've read it's a bug that's been around since Office 2003 or something.我不知道为什么会这样,但从我读到的内容来看,这是自 Office 2003 或其他版本以来一直存在的错误。

So now all I have to do is make sure that everyone uses multiple instances.所以现在我要做的就是确保每个人都使用多个实例。 Unfortunately you have to do some register changes to make that happen automatically.不幸的是,您必须进行一些寄存器更改才能自动进行。 I'm currently going with the .bat file alternative.我目前正在使用 .bat 文件替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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