How to insert a complex formula in a cell with VBA

  • Thread starter Thread starter mircea
  • Start date Start date
M

mircea

How to insert a formula in a cell with VBA
example : if formula is "=if(a3=2;a3;a2)"
i used
cells(2,3).Formula="=if(a3=2;a3;a2)"

bu i receive an error
Application object ....

if i used "=s4"
everything is ok.
 
Just a technique would be to place the equation into a cell manually just to
have Excel take a look at it.
If Excel doesn't complain, then copy the formula back into the macro. In a
more complicated statement, Excel might correct any missing '( ' or ')' .
Excel will correct any small letters like 'if' and 'a1' to the proper case
if all checks out.

Cells(2, 3).Formula = "=IF(A3=2,A3,A2)"

Again, just a technique.
 
Something I just learned - you can use a more "direct" code to do this using
"Iif."

Cells(2, 3) = IIf(Cells(3, 1) = 2, Cells(3, 1), Cells(2, 1))
 

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