简体   繁体   English

Excel / VBA模型在多次运行中会减慢速度

[英]Excel/VBA Model slows down over multiple runs

I have an excel based model that I'm using for my dissertation. 我有一个基于excel的模型,用于我的论文。 I've modified it quite a bit from the original in order to speed it up but I've just about given up optimising it now. 为了加快速度,我已经对其进行了很多修改,但是现在我已经放弃了对其进行优化。 The model opens excel workbooks and copies data across and closes them etc. 该模型将打开excel工作簿,并在其中复制数据并关闭它们,等等。

I'm looking at trying to do about 1000-2000 simulations with this model yet the time taken for the model to run goes from about 60secs for the first few runs upto over 100secs after about 60 runs. 我正在尝试使用此模型进行大约1000-2000次仿真,但是模型运行所需的时间从前几次运行的大约60秒增加到大约60次运行的100秒以上。 If I close and reopen excel then the time drops again. 如果我关闭并重新打开excel,那么时间会再次减少。 I've read around in optimising VBA code and tried to implement as much as I can. 我已经阅读了有关优化VBA代码的内容,并尝试尽可能多地实现。 I've put in 我已经放入

Application.CutCopyMode = False

at the end of each loop to clear the clipboard each time. 在每个循环的末尾每次清除剪贴板。 I've then since tried to bypass the clipboard by things like 从那以后,我就尝试通过诸如

Workbooks("Master").Range().value = Workbooks("Module").Range().value

rather than copying and pasting. 而不是复制和粘贴。 Yet all of this seems to have had little impact on the running of my model that still gets slower and slower. 然而,所有这些似乎对我的模型的运行几乎没有影响,而模型的运行速度越来越慢。

Any ideas as to what I can try next? 关于下一步可以尝试的任何想法?

=========Edit =================== =========编辑==================

Here's two pastebins for the code: 这是代码的两个粘贴框:

Master Spreadsheet has the code which steps through and runs each module 主电子表格具有逐步运行每个模块的代码

SubCatchment Module is an example module that is run by the master 子汇水模块是主模块运行的示例模块

Off-hand, here are the problems that I see: 临时而言,这是我看到的问题:

  1. you are using the Activate and Select methods, which are both slow and unreliable, 您使用的是Activate和Select方法,它们既缓慢又不可靠,

  2. the workbooks may have code of their own running when they are opened or activated, 当工作簿打开或激活时,它们可能具有自己运行的代码,

  3. you are not disabling calculations, which is both slow, and has a tendency to result in exactly the kind of "runaway run-time" you are seeing for workbooks with very complicated cross-linked calculations 您不会禁用计算,这既很慢,又有可能导致您所看到的具有非常复杂的交叉链接计算的工作簿的“失控运行时间”

  4. you are using potentially inconsistent string names for open/activated/executing workbooks instead of a single workbook variable for each one, making it uncertain if they are all getting closed when they should be, and 您正在为打开/激活/执行的工作簿使用可能不一致的字符串名称,而不是为每个工作簿使用单个工作簿变量,从而不确定它们是否应在需要时全部关闭,并且

  5. you have no error-handling or detection, but you are disabling error-reporting in various places, and so you can have no real idea if anything is going wrong down in the middle of your looping. 您没有错误处理或检测功能,但是在各个地方都禁用了错误报告功能,因此,如果在循环过程中出现任何错误,您将一无所知。

That's all at first glance. 乍一看,这就是全部。 I'm not inclined to spend more time going any deeper, when all of these issues are open and themselves sufficient to explain any amount of problems. 当所有这些问题都公开且足以说明任何数量的问题时,我不愿意花费更多时间进行更深入的研究。

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

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