简体   繁体   中英

Excel/VBA Model slows down over multiple runs

I have an excel based model that I'm using for my dissertation. 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.

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. If I close and reopen excel then the time drops again. I've read around in optimising VBA code and tried to implement as much as I can. 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,

  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.

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