Post permalink

The student's approach looks good to me. It is a very clean approach to initializing the test conditions and running the simulation.

The largest difference between the student's version and my code is that I actually write the test cases out to the worksheet as records, but that is not necessary for generating the histogram. I definitely misinterpreted the requirements in this respect.

With respect to the student's code, adding a DoEvents call in TestRun's For loop will keep Excel from locking up while the tests are running (see my code as an example -- you only need to call it periodically, not for every iteration of the loop). This might
be useful in case you wanted to watch the histogram Chart evolve over the course of the simulation in realtime (assuming that a Chart is predefined within the workbook), which is actually pretty neat to see.

For the AllocateFrequencyToTotalTravelTimeClassBins method, I wanted to point out that all of the If statements are currently being executed for each test iteration, but this does not have to be the case. For example, the following:

This would result in less work being done on each iteration, and it might allow for the simulation to run slightly faster. In general, it is ideal to minimize the amount of work being performed within a loop when possible.

Finally, I see that the student is writing a constant value to a cell to induce the worksheet to recalculate, thus allowing RANDBETWEEN to generate new values:

Cells(2, 3) = 10

The more explicit way to recalculate the worksheet through code is to fire the worksheet's Calculate() event:

Call Worksheets("Sheet1").Calculate

Certainly, this causes the same result to occur in the end, but it is easier to interpret what the code is doing -- the method is named to allow the code to be self-documenting in nature.