Working with formulas using the Counter and Offset Property

sjv5150
04-18-2008, 09:38 AM
I have a code that loops through 240 cells to input the following formulas from a worksheet called "Performance." The looks at the values of the cells in the "Performance" worksheet and flags them (returns a value of 1 or 0) based on the 4 different scenarios. I am running into a problem with the speed of the macro. It is super slow. It actually is pasting the formula into each indidvidual cell 240 times (takes about 45 seconds). Even with the ScreenUpdating set to False, it still takes a long time. I can't copy/paste the formulas either because Excel can't recognize the pattern. There must be a more efficient way of performing this. Any help will be much appreciated.

For Counter = 1 To 60
g = ActiveCell.Row
h = ActiveCell.Column
i = ColNo2ColRef(h) ' From Function
h = h + 1
j = ColNo2ColRef(h) ' From Function

ws2.Activate
Selection.formula = "=IF(Performance!" & i & g & ">0,IF (Performance!" & j & g & ">0,1,0),0)"
Selection.Offset(0, 1).Activate
Selection.formula = "=IF(Performance!" & i & g & ">0,IF(Performance!" & j & g & "<=0,1,0),0)"
Selection.Offset(0, 1).Activate
Selection.formula = "=IF(Performance!" & i & g & "<=0,IF(Performance!" & j & g & "<=0,1,0),0)"
Selection.Offset(0, 1).Activate
Selection.formula = "=IF(Performance!" & i & g & "<0,IF(Performance!" & j & g & ">=0,1,0),0)"
Selection.Offset(0, 1).Activate
ws1.Activate
Selection.Offset(0, 2).Activate
Next

MPi
04-18-2008, 04:45 PM
Try not to use the "Activate" or "Select" keywords.

Prefer this way
Range("C" & i).Formula = "..."

You can also use Application.Calculation to put it to Manual before doing the loop and to Automatic after it's done.

sjv5150
04-21-2008, 10:19 AM
Thanks for your time. I'll check it out

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum