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

This should fit the bill:
Sub RandMacro()
' Macro by Bernard V Liengme
Range(ActiveCell, ActiveCell.Offset(rowOffset:=10,
columnOffset:=0)).Select
Selection.FormulaR1C1 = "=RAND()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
End Sub


Just change the "rowOffset:=10" part to "rowOffset:=1000"

With Excel running and the required file open, copy the code above from you
email program.
Use Tools|Macro-Visual Basic Editor (or press ALT+F11)
In the VB Editor, click the Insert menu item and then the Module item from
the drop down menu.
Paste the code into the work space.
Return to Excel; move to the first cell where the RAND function is needed.
Use Tools|Macro-Macros and from the dialog box click the RandMacro entry

A quick way to copy formulas when you have a column of cells (Say A1:A100)
with something entered in each: type the formula in the cell next to the top
entry (in this case in B1) and now double click the fill handle (left black
box in lower right corner) of B1. Hey pesto! the stuff gets copied all the
way down.

Come back if you have questions. Reading a book is a great idea!

--
Bernard V Liengme
www.stfx.ca/people/bliengme
remove caps from email

wrote in message
ups.com...
Can someone provide a VBA function to populate 1000's
of cells with a simple expression, for example =RAND()?

That is, I want to have a program/macro to populate the cells
instead of having to replicate them manually (e.g, by dragging),
which is painfully slow for such large numbers of cells. If
there is an alternative that is equally fast and easy to use,
I would appreciate hearing about it.

I would also appreciate step-by-step instructions for how to
enter the macro and how to execute it.

And since I am interested in an expression involving RAND(),
I would appreciate advice on how to prevent re-execution of
the macro each time the spreadsheet is recalculated -- other
than turning off automatic recalculation. (But if that is the
only way, I would appreciate confirmation.) Or is that the
norm; i.e, are macros executed only when you invoke them
explicitly from the keyboard?

I know I should read a book, and I will in time. But I am
hoping this is a simple enough request that someone will
not mind filling in the blanks sooner than I digest a book.

Thanks.