PDA

View Full Version : Solved: Setting a range to the result of a formula



someboddy
08-29-2010, 01:57 PM
I want to set a range to the result of a formula - that means that every cell in that range will be set to the result of that formula applied to that cell. For example: if I have a formula "=RC*3" every cell in that range will be multiplied by 3.

Now, I know I can easily do it with a loop, but my range contains tens of thousands of cells, and I know Excel is optimized to do a similar action on a large number of cells. I can probably create a temporary sheet for this, but if there's a build in function that can do it my life would be much easier.

mikerickson
08-29-2010, 02:09 PM
The technique would depend on what formula you are using.
In the example you gave, put 3 in a spare cell, select your range, PasteSpecial with Multiplication checked.

In general, a loop would be the general solution. But not a loop that writes to the cells.
Dim myCells as Range
Dim newValues as Variant

Set myCells = Range("A1:AZ1000")
ReDim newValues (1 to myCells.Rows.Count, 1 to myCells.Columns.Count)

For i = 1 to myCells.Rows.Count
For j = 1 to myCells.Columns.Count
'myCells.Cells(i,j) = 3 * myCells.Cells(i,j): Rem bad
newValues(i, j) = 3 * myCells.Cells(i,j): Rem good
Next j
Next j

myCells.Value = newValues:Rem continuation of goodOne bulk write to cells is much faster than many one cell write to cell operation.
Looping to fill an output array and then put that array into the range is the quickest general solution.

Specific situations may have a quicker implementation.

someboddy
08-29-2010, 02:36 PM
Thanks!