+ Reply to Thread
Results 1 to 16 of 16

Problem getting VBA to recalculate and store results within a loop then outputting results

  1. #1
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Problem getting VBA to recalculate and store results within a loop then outputting results

    Hi Experts, I'm a novice at VBA and am trying some simple code to generate some random numbers, store the results in an array and then output them on another sheet. Not sure if it's not recalculating each time or if it's the way I'm outputting the results but I get 100 columns of identical numbers instead of 100 columns of "random" numbers. Would appreciate any help! Here is the code:

    Option Explicit
    Option Base 1
    Sub myMacro2()

    Dim vData As Variant
    Dim dOutput() As Variant
    Dim j As Long
    Dim k As Long

    Application.ScreenUpdating = False
    'Application.Calculation = xlCalculationManual

    ' Assign the range to a variant containing an array.
    vData = Worksheets("Input2").Range("B2:B21")

    ' Set up the result array.
    ReDim dOutput(UBound(vData, 1), 100)

    ' Generate the numbers.
    For k = 1 To 100
    With Worksheets("Input2")
    .EnableCalculation = False
    .EnableCalculation = True
    .Calculate
    End With
    dOutput = vData
    Next k

    ' Return results by assigning the result array to the range.
    Worksheets("Results2").Range("B2"). _
    Resize(UBound(dOutput, 1), 100) = dOutput

    'Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = True

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    You do realize that putting the "dOutput = vData" inside the for / next loop, ensures that in the end your dOutput is only the 20 'random' numbers.
    This would totally explain why you end up with 100 columns of the same numbers.

    But why do you put random numbers on the worksheet and then calculate?
    Why not:

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    That would give you actual random numbers without having to calculate the worksheet.
    Please click the * below if this helps

  3. #3
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Hi Jasper,
    I'm sorry, I wasn't too clear - the "random" numbers are proxies for Monte Carlo simulation code that I will want to calculate so many iterations and capture the results each time as an array and only outputting all the iterations at the very end to avoid all the slow writes to a worksheet for each iteration. So the above goal was to capture the 20 values each time in a separate column and then at the end output all of them to an output sheet. Actually, I would like to be able to transpose the values when placing them on the output sheet so instead of 20 rows by 100 columns I would end up with 100 rows by 20 columns? Any help would be great!
    Attached Files Attached Files
    Last edited by kmarchell; 05-09-2015 at 03:47 PM. Reason: Added attached file

  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Upload a sample worksheet, please

  5. #5
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Jasper, I attached the file to my prior post - sorry new to site and not too familiar with it yet. Thanks for your help!

  6. #6
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    You're using randbetween(1,100) to generate your random cells.
    Then why would you do that at all?

    Please Login or Register  to view this content.
    Fills 100 rows by 20 columns with random numbers between 1 and 100...

  7. #7
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    The random numbers are just "placeholders" for what the Monte Carlo spreadsheet will calculate and don't represent the actual values. Instead of copying the results each iteration to a results sheet I'm just trying to store the results (20 values) each time the spreadsheet calculates (100 times) into an array and then put all the arrays in the results sheet after all the iterations are done. The current way I'm doing it takes about 2 minutes for 2000 iterations in Excel 2010 (took 40 seconds in Excel 2003) and I'm just trying to speed it up by not copying the results on to the results sheet until everything is finished.

  8. #8
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    How about this:
    Please Login or Register  to view this content.
    Tried it a bunch of times and it each time clocked in under a second:
    0.46875
    0.71875
    0.453125
    0.4453125
    0.6796875
    0.4609375
    0.46875
    0.40625
    0.4453125

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Is this what you wanted?7
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Jasper,
    I'm trying to avoid writing to the sheet with each calculation (iteration) - that is the way it is being done now in the current model. Instead I"m trying to find a way to calculate the sheet, store the results (represented by B2:B21 in the example) in an memory array, recalculate the sheet, store the results in the next column of the memory array, and repeat this for a total of 100 times. So will end up with 100 columns of results that will then be copied (& transposed) on to the results sheet. Doing everything in memory versus all the writing to the results sheet will speed it up a lot but I just can't seem to get the loops to do what I want. Any other suggestions would be greatly appreciated!

    Jindon - the application.transpose suggestion did exactly what I wanted for the final write. Now, just need to find away to store each iteration in memory until the count is finished as described above. Thanks for the help!

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Sample code
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Jindon, I like the code but I need to use the calculated results from the input2 sheet for each iteration. I was just using the random numbers as placeholders to test whether I would get new numbers each time - the real calculated results I'm trying to capture at each iteration are not random numbers so I can't use the formula like you inserted but have to capture the results (20 cells) at each iteration for the 100 iterations and then transpose the results and put on the results sheet.
    In reply 6 for a similar problem posted on the link below - ChrisM indicates:
    1. create a temp array to hold one loop's data (you can't assign directly from the worksheet to a set row of the big array)
    2. Each time the loop runs write the results to the temp array
    3. Write the temp array row to the big array
    4. When all done, write the entire big array to worksheet
    http://http://www.mrexcel.com/forum/excel-questions/692324-copy-paste-value-loop-within-visual-basic-applications-array.html
    I'm having trouble converting his code to something that will work in my example. Appreciate your help and patience with a newbie like me. Kirk

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    As I mentioned, it was just a sample, because I don't understand what you are really trying to do.

    If the calculation will populate the columns of data in input2 column by column, why not just copy/pastespecial with transpose protocol set to true after 100 columns?

    Am I missing something?

  14. #14
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    The more complicated spreadsheet contains other sheets that feed a master calculation sheet that calculates results. I need to capture those results (say B2:B21) each time the spreadsheet is calculated (say for 100 times). I had previously used vba to capture the results at the end of each calculation and paste the results onto a results sheet but since Excel 2010 is much slower than Excel 2003, was trying to find a way to avoid pasting the results until all 100 recalculations were done. Thus, I am trying to capture the results from those cells (B2:B21) each time the complicated spreadsheet recalculates, store them in an array in memory, and when all 100 recalculations are done and stored - place the results of all 100 recalculations on to a results sheet for additional processing and charting.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,575

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Then you need to manage them like
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    04-12-2015
    Location
    Houston, TX
    MS-Off Ver
    2003
    Posts
    13

    Re: Problem getting VBA to recalculate and store results within a loop then outputting res

    Jindon, Thanks very much - that seems to do exactly what I needed - I will try using it in my complicated spreadsheet and see how much it speeds it up. I appreciate all your help and patience! I don't use VBA very often so have never gotten very adept at it - glad there are experts like you willing to help!

    Update: By writing to the memory array instead of to the file in each iteration, calculation time was improved from ~2 minutes to 20 seconds, an approximate 6 fold decrease in time!
    Last edited by kmarchell; 05-14-2015 at 08:56 PM. Reason: Added performance improvement information

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Change Input - Recalculate - Paste Results for all Inputs Separately
    By RiteshChaturvedi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-21-2013, 09:57 AM
  2. recalculate formula in field based on what results are displayed
    By blackspiral in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2013, 12:27 PM
  3. Replies: 3
    Last Post: 03-31-2010, 11:47 AM
  4. [SOLVED] text box to contain formulae results that recalculate
    By Jack Sons in forum Excel General
    Replies: 2
    Last Post: 10-21-2005, 09:05 AM
  5. Comparing contents of two spreadsheets and outputting results to a
    By brx in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-16-2005, 10:06 PM

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