Hey all,
I could use some feedback on ways to optimize a bit of VBA code (below) for speed. I have written a fairly complex model in a single workbook that analyzes and reports the thermodynamics and economic performance of a renewable energy system. The workbook has something like 40 sheets, but for this question, only two are relevant: TEMs and Periodic. The TEMs sheet contains an hourly snapshot of the thermodynamics of the system. It currently has 3 dynamic inputs, which change every hour. It also has about 20 or so static inputs (static in the sense that they don’t change during the program run, but may change from run to run). Some of the static parameters are in the sheet, and some come from other sheets in the workbook. I need to generate a table of up 24 output values on the hourly basis, which means it has 8760 rows for a year’s worth of data. The Periodic sheet contains the 8760 rows of inputs, and it’s also what receives the 8760 rows of outputs.
The VBA code below is what I’ve written, which after a number of versions, I think I have stripped it down to the barest essentials. Formerly, I used copy/paste commands, but I think this method of direct cell writing – on what is essentially a big data movement exercise – is faster. The process is simple: put the input data into TEMs, recalculate only that sheet, put the resulting output data into Periodic. Then transfer an ending value (one of the outputs) into the next period’s starting value (now it’s an input), and do it again 8760 times.
Currently, the program takes about 20 minutes to complete. In a previous version, when I was tracking only about 14 outputs, it took about 3 to 4 minutes. This is when no other programs are running, and Excel is left as the primary window. The overall workbook is about 3.5 MB in size. I’m using a 1 year old Lenovo Thinkpad, running Win7 Pro 64 bit, with 6GB RAM. The processor is an Intel(R) Core(TM) i7-2720QM CPU operating at 2.20GHz (4 processor cores).
Any suggestions you have to speed this up would be appreciated. Ideally, I’d like to see this program run in 1-2 minutes or less. Alternatively, if you think this is the most optimal code, I’d appreciate hearing that too, as I am far from an expert on this stuff.
HERE's THE CODE
![]()
Please Login or Register to view this content.
Bookmarks