View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.misc
 
Posts: n/a
Default Freezing the cell (with Random formula)

"Christy" wrote:
Everytime I copy the cell (wth rand() formula)
and paste it to another cell (use paste special -
value - ok), the value of the original cell changes.
How can I freeze the value of the original cell?


This is a very reasonable request. There is nothing
antithetical with the concept of RAND().

You might try searching for the discussion entitled "VBA
code to populate 1000's of cells" in the excel.misc or
"General Questions" newsgroup. The example is indeed
about using RAND() in a manner that does not recalculate
automatically without having to disable auto recalculate
across-the-board, which is generally undesirable to do.

Three responders -- JE McGimpsey, Bernard Liengme and
David McRitchie -- each provide important pieces of the
solution. You might want to take a look at all three
postings, since each one selects the cells slightly differently.
By the way, Bernard posting has excellent step-by-step
procedures for entering a macro, if you are unfamiliar
with doing that.

The following is my summary. Click Tools Macros
Visual Basic Editor or type alt-F11. In the VBE, click
Insert Module and enter the following macro:

Sub MyRand()
For Each cell In Selection
cell.Formula = "=RAND()"
cell.Value = cell.Value
Next cell
End Sub

(Note: I am a VBA novice. There might be more
elegant solutions.)

Close the window. You can rename the module name
by clicking "Module1" in the lower left window and
typing "MyRand". Close the VBE window.

In the spreadsheet, select the desired cells, type alt-F8,
highlight the MyRand macro (if necessary) and click Run.
Voila! The cells contain random values, not the formula
"=RAND()". Obviously, those values will never change
unless you select those cells and execute the macro again.

JE and Bernard show ways to code the macro so that
the random values always go into a fixed range, if that
is what you want.