Using formula in code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following formula which works fine in a worksheet:

=IF(AND(N4>-1,N4<31)=TRUE,"1-30",IF(AND(N4>30,N4<61)=TRUE,"31-60",IF(AND(N4>60,N4<100)=TRUE,"61-99",IF(N4>99=TRUE,"100+","Error!"))))

However, when I try to use VBA code to enter the formula using this ( which
is basically the same thing)

Range("S2").FormulaR1C1 = _
"=IF(AND(RC[-1]>-1,RC[-1]<31 = TRUE,"1", _
IF(AND(RC[-1]>30,RC[-1]<61 =TRUE,"31-60", _
IF(AND(RC[-1]>60,RC[-1]<100) =TRUE, "61-99", _
IF(RC[-1]>99 = TRUE,"100+"."ERROR!"))))

I get a syntax error message

Can anyone tell me how to get it to enter the formual automatically please?
 
Hey Alan,

Try this

Range("A6").Select
ActiveCell.FormulaR1C1 = _

"=IF(AND(R[-1]C[1]>-1,R[-1]C[1]<31)=TRUE,""1-30"",IF(AND(R[-1]C[1]>30,R[-1]C[1]<61)=TRUE,""31-60"",IF(AND(R[-1]C[1]>60,R[-1]C[1]<100)=TRUE,""61-99"",IF(R[-1]C[1]>99=TRUE,""100+"",""Error!""))))"
Range("A6").Select

Regards
Subhash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top