randomexcel-formulaexcel-2007excel-matchnon-volatile

MS Excel - Run formula or save previous value based on random row selection


I have an Excel sheet for which I am selecting random rows based on bands of numbers. My initial goal was to select a row and save the time at which it was selected by the random number generator. However, due to recalculation and volatility of the rand() function, I had not been able to save selection times for all rows; only the current one.

  |  A   |     B       |      C      |  D  |E|F|  G   |  H   |     I    |
 -+------+-------------+-------------+-----+-+-+------+------+----------+
 1| Name | Time Save   | Time        | Blk | | | Rand | Item | Itm Name |
 2| A    | 0           | 0           | 0   | | | 727  | 8    | H        |
 3| B    | 0           | 0           | 100 |
 4| C    | 0           | 0           | 200 |
 5| D    | 0           | 0           | 300 |
 6| E    | 0           | 0           | 400 |
 7| F    | 0           | 0           | 500 |
 8| G    | 0           | 0           | 600 |
 9| H    | 42814.58888 | 42814.58888 | 700 |
10| I    | 0           | 0           | 800 |
11| J    | 0           | 0           | 900 |

Initially I thought that perhaps the value() function would simulate the manual Paste Special > Value command and effectively "freeze" the current value, but this was not working (no real surprise).

Before figuring out what I was doing wrong, I was able to type F9 repeatedly and successfully watch the floating-point timestamp jump around randomly in the C column and its value increase as expected. Additionally, this would be mirrored in the B column. However, previous times would not be saved and instead reset to 0.


Solution

  • I eventually found that circular references solved my problem. Instead of using =VALUE(Cx) in the B column, I finally hit on the idea of testing whether or not the current value in the C column was equal to 0 (not selected), and then using a circular reference, saving the previous value of B if it was 0, or updating if it was not:

    Bx: =IF(Cx>0,Cx,Bx)

    Obviously, this gave the standard Circular Reference warning, but since this was intentional, I bypassed it. Now I see:

      |  A   |     B       |      C      |  D  |E|F|  G   |  H   |     I    |
     -+------+-------------+-------------+-----+-+-+------+------+----------+
     1| Name | Time Save   | Time        | Blk | | | Rand | Item | Itm Name |
     2| A    | 42814.62031 | 0           | 0   | | | 159  | 2    | B        |
     3| B    | 42814.62052 | 42814.62052 | 100 |
     4| C    | 42814.62021 | 0           | 200 |
     5| D    | 42814.62050 | 0           | 300 |
     6| E    | 42814.62043 | 0           | 400 |
     7| F    | 42814.62045 | 0           | 500 |
     8| G    | 42814.62046 | 0           | 600 |
     9| H    | 42814.62021 | 0           | 700 |
    10| I    | 42814.61980 | 0           | 800 |
    11| J    | 42814.62015 | 0           | 900 |
    

    Add some fancy color-scale conditional formatting on the B column and hold down F9, and I can see cells gradually grow redder and redder until they repaint themselves green upon being selected.