Pointers needed for speeding up nested loop macro in VBA

后端 未结 3 1228
迷失自我
迷失自我 2020-12-07 05:44

I need help speeding up my straightforward, 8-variable, nested loop macro. Each loop still takes ~1 second and there are a few hundred thousand loops to completion so it ta

3条回答
  •  借酒劲吻你
    2020-12-07 06:35

    Remember that each time you move/assign a value to a cell on a worksheet, Excel stops and recalculates the whole sheet again. @Joe's link to turn off this auto-calculation while you move your data will give you some speed boost. But...

    Your loops would speed up tremendously if you perform all your calculations in the VBA routine instead of copying them to your worksheet and relying on your formulas to perform the calculations. Also, using memory-based arrays to hold your results can minimize how much your routine slows down for this copy. (Chip Pearson has a very good explanation of this.)

    So my recommendations:

    1. Use the loop variables (w, a, x, ...) to perform your calculations within your VBA routine, i.e. move the formulas into your code.
    2. Store the results in a memory array Dim results(1 to 44) as Double
    3. Copy that results array directly to the next area of your results range (g15:ax15).
    4. Or better yet, append this result array to another memory array Dim allResults(1 to 1000, 1 to 44) as Double -- (hint: you can't do this in a single statement, you'll have to loop to copy from the results array to the next slot in the allResults array). Then block copy the allResults array to a Range on your worksheet.

    You should get a large speed boost from these techniques.

提交回复
热议问题