View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
L. Howard Kittle
 
Posts: n/a
Default VBA code to populate 1000's of cells?

How can I make 10 a "variable" -- an input or parameter to
the macro or a reference to a cell value?


Try this change to Bernard's code. (You might want to use Long instead of
Integer.)

Sub RandMacro()
' Macro by Bernard V Liengme

Dim I As Integer
Dim J As Integer
I = Range("a1").Value
J = Range("a2").Value

Range(ActiveCell, ActiveCell.Offset(rowOffset:=I, _
columnOffset:=J)).Select
Selection.FormulaR1C1 = "=RAND()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
End Sub

HTH
Regards,
Howard

"nomail1983" wrote in message
...
Thanks so much to all -- JE, Bernard and David and anyone
else that follows.

JE and David (or anyone else), can you comment on VBA Rnd
v. Excel RAND(). Are they the same algorithm? if not, is one
known to be better than the other?

Bernard, your instructions were impeccable. I can correct
the off-by-one error (rowOffset:=10 yields 11 cells, not 10).

How can I make 10 a "variable" -- an input or parameter to
the macro or a reference to a cell value? I can live with
editing the macro for now; or I can use David's idea with
"for each ... in selection". But I thought this should be a
simple embellish to Bernard's macro (famous last words!).

Borrowing FE's formula syntax (thanks very much) and taking
a wild guess (I know: RTFM!), the following did not work.
But it demonstrates what I want to do -- and of course, I am
open to any better ideas.

Dim mysz as long
mysz = "=IF($A$1 0, $A$1, 10)"
Range(ActiveCell, ActiveCell.Offset(rowOffset:=mysz,
columnOffset:=0)).Select

I tried various incarnations with ".Formula" and ".Value", even
a hypothetical Value(...) function, to no avail. Obviously I need
to get that book or study one of the tutorial web sites.

In any case, your responses have been a great motivator, and
it gave me a head start. Thanks again.