+ Reply to Thread
Results 1 to 15 of 15

Feedback / ideas for optimizing this VBA code for speed

  1. #1
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Feedback / ideas for optimizing this VBA code for speed

    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.
    Last edited by JosephP; 01-10-2013 at 09:45 AM. Reason: code tags

  2. #2
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Feedback / ideas for optimizing this VBA code for speed

    Please Login or Register  to view this content.
    at the very begining.
    Dont use variant if its not needed.
    If you think that my answer was helpful, please click on the "Add to this user's Reputation" button.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Feedback / ideas for optimizing this VBA code for speed

    Quote Originally Posted by Bishonen View Post
    Dont use variant if its not needed.
    Or just don't declare variables if you're not going to use them.

    Also, you don't need to use Evaluate to get the result of simple expressions.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    untested but maybe
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Feedback / ideas for optimizing this VBA code for speed

    This is very interesting. But I'm stuck on the line in the For i-1 to 8 section:

    .Range("HM_Input_" & Chr(64 + i)).Value = HM_Input(Period, i)

    I'm getting a "Type Mismatch" error, and the right side of the equal sign appears to be the culprit: Watching that expression yields a Type Mismatch that I don't quite understand.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    maybe change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    or provide a sample workbook for testing

  7. #7
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Feedback / ideas for optimizing this VBA code for speed

    I guess I'm confused by the "HM_Input(Period, i)" expression to begin with. "HM_Input" has already been defined as a variant variable. And it was given a very workable (and the correct one from my Watch window) value just 3 lines prior. So what's the "(Period, i)" for in the expression? I tried running it with that part stripped off in order to just try to assign its recently acquired value to the location. But now the error that comes back as "Application-defined or object-defined error".

    Sorry, the workbook is company-confidential, so I'm not at liberty to let it out. I otherwise like your approach a lot, and am hoping to use it.

  8. #8
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Feedback / ideas for optimizing this VBA code for speed

    Oh, and the HM_Input(1, i) also got a type mismatch error

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    Please Login or Register  to view this content.
    creates an array from each row of the input range. the line in error should definitely be using the amendment I made because it's only one row each time

    do you have any error values in that range?

  10. #10
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Smile Re: Feedback / ideas for optimizing this VBA code for speed

    There are no errors in the input values.

    In order to preserve confidentiality, I've extracted the two relevant sheets and uploaded the file. I've changed the formulas in the TEMs sheet (the hourly calculator) to reflect a simplified algorithm to track results instead of the confidential calculations. The relevant named ranges are identical. However, I've added "WhatPeriod" just so I can track this test version.

    You can see the type of results I'm looking for in the first three rows in the Outputs section of the Periodic sheet. I've inserted your code as the workbook's only macro. I am still getting the same error result either with HM_Input(Period, i) or HM_Input(1, i): type mismatch.

    I'm still confused as to why this error is occuring. Your help is really appreciated!!
    Attached Files Attached Files

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    it's because your HM_Inputs and HM_Outputs ranges actually only refer to one cell which was not at all clear from the code. change the code to
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Feedback / ideas for optimizing this VBA code for speed

    "HM_Inputs and HM_Outputs ranges actually only refer to one cell " - yes, that was an artifact left over from the previous method. If I had them referring to the entire 8760 row range, would that have made the difference for your previous code?

    So this latest version works great in the original workbook. The execution time is still 20 minutes. I still like the programming exellence of your method, and I've wanted to learn to use variable arrays for awhile, so thanks for giving me a practical entry.

    But, is this as good as it gets? 20 minutes to complete? Is there any other kind of coding approach you would suggest I explore?

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    with the ranges being only one cell the variable was not holding an array-just a value-which is why the type mismatch arose. if the ranges had been tables it would have run ok as originally coded

    I can't comment on speed without knowing the specifics of the actual workbook but I suspect you could store all the results in an array and write that out once at the end-but wasn't sure as you seem to use at least some of the output as input in subsequent calcs-or possibly do all the calculations in the code which might be faster. it sounds like a long time for so few rows though-perhaps calculating specific ranges only would assist

  14. #14
    Registered User
    Join Date
    01-10-2013
    Location
    Denver, CO, USA
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Feedback / ideas for optimizing this VBA code for speed

    Do you know if the major process-time-consumer is the worksheet calculation; or is it the act of transfering the results?

    I'm inclined to try to put the results to an array and write it out at the end, as you suggest. I do only have one result that gets transfered from one period to the next, but I suspect I could still perform that function within the array variable - right? But all this assumes that the major time-suck is writing/transfering results from the TEMs calculations to the Periodic table.

    However, if the worksheet calculation is mostly what's slowing it down, then the array strategy wouldn't be worth the time/effort to figure it out. I don't think I can put the TEMs calcluations in code because of they way I need to use them to graphically display the dynamics of the energy system. This also allows me to change the system structure and calculations (and visually keep track of what I'm doing), which I will need to do on more than a rare occasion.

    I've now attached a stripped down version of the TEMs (all calculations are real, but all labels and system drawings are removed for confidentiality) which is where all the periodic calculations are done for this macro. You'll note I only allow the manual calc of that sheet in each period, not the entire workbook. As you can see, it has the three dynamic inputs, and a bunch of other static inputs. You can also see how the math is all linear, and the formulas aren't too complex - all fairly simple stuff. The macro is also included (limited to six periods) and is functional.

    Appreciate your insights here.
    Attached Files Attached Files

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Feedback / ideas for optimizing this VBA code for speed

    extend that sample file to the same number of rows as your real model-how fast does it run? it seems pretty quick to me with 1000 iterations even on a mac

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1