Run-time error 1004

  • Thread starter Thread starter Riccardo Pucher Prencis
  • Start date Start date
R

Riccardo Pucher Prencis

The code that I am trying to use is very simple.
I would like to replicare automatically a formula along many cells, one yes
and one no along a given range.
So I tried to build a macro:
sub a ()
for i 4 to 400 step 2
range(i).formula="=if(a2<b2;a2;b2)
next i
end sub
The macro doesn't work even without the for-next cicle.
On the contrary, the formula =if(a2<b2;a2;b2) works as expected when placed
in a cell of the worksheet inside which I want to run the macro.
What am I doing wrong?
Thank you for your help,
Riccardo
 
Not sure that the following is really the answer you require.

Firstly my example uses the comma (,) as a delimiter in the formula so I am
not sure if the semi colon (;) is causing you a problem or perhaps you use
that in your country of origin.

When you want to use a formula with relative addressing then you need to
code the formula in the first cell then the next line of code copies that
cell to the remaining cells something like the following.

Range("A3").Formula = "=IF(A2<B2,A2,B2)"
Range("A3").Copy Destination:=Range("A3:AH3")

If this does not answer your question then perhaps you can supply a little
mor explanation with an example of your data.

Regards,

OssieMac
 
VBA is USA centric.

Use commas instead of semicolons.

But I'm confused about what Range(i) is.



sub a ()
for i = 4 to 400 step 2
range(i).formula="=if(a2<b2,a2,b2)"
next i
end sub
 
To take what Dave Peterson a step further: if you are using i as a row
counter, then you probably need to change the line that sets up the formula
from
Range(i).Formula = ...
to
Range("C" & i).Formula = ...
assuming C is the column where you want to put the formula.
 
Back
Top