+ Reply to Thread
Results 1 to 7 of 7

Monte Carlo Simulation macro (update random values, calculate results, copy/paste, repeat)

  1. #1
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Monte Carlo Simulation macro (update random values, calculate results, copy/paste, repeat)

    Hi all,

    I've build a simulation model. It would be really helpful if I had a macro that would allow me to run the model multiple times, and each time:

    - Update the random values before calculations
    - Save the results in a different column each time (as not to paste over the results from previous runs)

    Here is the file: Monte Carlo Simulation.xlsm

    In sheet UI cell A1 you'll find the number of runs (repeats). So essentially the macro should stop once it has run the model X number of times. The model itself runs by activating the Sub Run_Model() that is already in the file.

    In cell B2 you'll find the seed increase. This value should be added to the seed values in cells C4, D4, E4 and F4 of sheet RANDOM. So after the first run C4 goes from 1 to 2, and D4 goes from 10 to 11. After the second run, C4 goes from 2 to 3, and D4 goes from 11 to 12, etc. This way the four list of random numbers will be updates before the next run.

    Thereafter the Sub Run_Model() macro should be run to calculate results. Finally (and this is the tricky bit), the results which can be found in the range B2:B5 of sheet OUTPUT should be copy/pasted to C2:C5 on the first run, but to D2:D5 on the second, and E2:E5 on the third, etc. So each time the model is repeated, the paste are moves to the right with one column.

    Can anyone add this second macro to the file?

    Please note: the attached file is not my actual model, but in terms of how the sheet relate to one another it is identical.

  2. #2
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Monte Carlo Simulation macro (update random values, calculate results, copy/paste, rep

    I'm not sure of your intentions.

    perhaps this code does what you need
    Please Login or Register  to view this content.
    Vicho Palacios

  3. #3
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Re: Monte Carlo Simulation macro (update random values, calculate results, copy/paste, rep

    Hi vichopalacios, thanks for your code. It seems to do the trick. I did replace the ''25' in the last line with ''nRounds'', because I suspect that 25 should represent the variable number of runs.

    As for my intentions, I want to determine the variability in the output data based on stochastic input data. When the input data is deterministic, then the output will always be the same. But because the input are multiple probability distributions, their interaction will create variability in the output. To capture that variability I needed a formula that would recalculate the model multiple times, using different series of random numbers, and copy he results. With this code I can do that, so thank you
    Last edited by Ronnet2; 10-04-2015 at 04:24 AM.

  4. #4
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Re: Monte Carlo Simulation macro (update random values, calculate results, copy/paste, rep

    Question: is it possible to add in a code to save the file before the next iteration line?

  5. #5
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511
    Quote Originally Posted by Ronnet2 View Post
    Question: is it possible to add in a code to save the file before the next iteration line?
    Hi Rennet
    Glad to know it worked for you
    My mistake using 25 where should be nRounds, sory for that, but you fixdd it.
    You mean to save the file after rumnnig the nRounds iterstions?
    What is your protocole to name those files to be saved?
    People use to save under the same string name plus a little ammend which chages every time following certain protocole
    Vicho

  6. #6
    Registered User
    Join Date
    03-26-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    75

    Re: Monte Carlo Simulation macro (update random values, calculate results, copy/paste, rep

    Quote Originally Posted by vichopalacios View Post
    Hi Rennet
    Glad to know it worked for you
    My mistake using 25 where should be nRounds, sory for that, but you fixdd it.
    You mean to save the file after rumnnig the nRounds iterstions?
    What is your protocole to name those files to be saved?
    People use to save under the same string name plus a little ammend which chages every time following certain protocole
    Vicho
    It can save over the same file, after each run. Due to your macro the results of each run will all be saved (in different columns), so its ok to save over the same file.

    The reason I wish to save between runs is because of the ''run_model'' macro. The actual model is much more complex than in this example file. Each run will take 1-2 hours, and I'll run the file for the next month every night and I fear Excel might crash somewhere along the way. So to avoid losing all the results, I'd like for the Excel file to save itself between runs. So should it crash during run 21, then at least I still have 20 runs of recorded results when I wake up

  7. #7
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Monte Carlo Simulation macro (update random values, calculate results, copy/paste, rep

    Ronnet2
    you can insert:

    Application.DisplayAlerts = False
    Thisworkbook.Save
    Application.DisplayAlerts = True

+ 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. Monte Carlo Simulation
    By mchl2121 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2010, 07:11 PM
  2. Monte Carlo Simulation
    By wmfinance in forum Excel General
    Replies: 3
    Last Post: 07-13-2010, 01:33 PM
  3. Monte Carlo simulation
    By Elijah in forum Excel General
    Replies: 9
    Last Post: 05-07-2010, 06:39 PM
  4. Monte Carlo Simulation
    By Phedwards in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2007, 07:30 PM
  5. monte carlo simulation
    By unique in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-16-2005, 10:05 PM
  6. How can i set up a MONTE CARLO SIMULATION macro in Excel? thx
    By MonteUser in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-14-2005, 11:06 AM
  7. How can i set up a MONTE CARLO SIMULATION macro in Excel? thx
    By MonteUser in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2005, 08:06 PM
  8. Monte Carlo Simulation
    By pkghosh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-13-2005, 12:12 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