Ben
Ben

Reputation: 123

How to put a formula into a cell by Excel VBA?

I'm trying to put this =IF(D49>0,D49-D50-D51+D52+D53,) into a cell D54 by using macro.

My code is as follow

Range("D54").FormulaR1C1 = "=IF(D52>0,D49-D50-D51+D52+D53,)"

But when I run the macro.

Cell D54 gets this =IF('D52'>0,'D49'-'D50'-'D51'+'D52'+'D53',) instead.

Excel adds quotation marks to each dells in the formula, render the formula useless. How can I get macro to into formula as formula?

Upvotes: 0

Views: 7026

Answers (2)

jpapadakis
jpapadakis

Reputation: 58

I think you are having issues because you are using the R1C1 formula property for your range, yet you are using classical cell references in your formula. You should be using the Range("D54").Formula property. So your code would look like:

Range("D54").Formula = "=IF(D52>0,D49-D50-D51+D52+D53,)"

Upvotes: 3

USFBS
USFBS

Reputation: 279

You could just use Range("D54").Value = "=IF(D52>0,D49-D50-D51+D52+D53,)"

I have used that method before and hand good results with it.

Upvotes: 0

Related Questions