niko
niko

Reputation: 9393

Unable to insert formula from vba

I want to insert these formula using vba but it throws me an error application defined error

activesheet.range("A4").formula = "=IF(AND($B4>TODAY() -7,$B4 < TODAY()),"ONE WEEK OLD",IF(AND($B4 < TODAY() -7,$B4 > TODAY() - 30),"ONE WEEK TO ONE MONTH OLD",IF(AND($B4 < TODAY()-30,$B4 > TODAY()-90),"1 MONTH TO 90 DAYS OLD","OLDER THAN 90 DAYS")))"

I have used concatenation and also using a string variables but still Im unable to do it.

I am unable to insert a formula in a cell if formula has double quotes in it from vba

Please help me with these Thanks

Upvotes: 1

Views: 5023

Answers (1)

Jon Egerton
Jon Egerton

Reputation: 41589

You need to escape the quotes in the string. To do this just double them up:

activesheet.range("A4").formula = "=IF(AND($B4>TODAY() -7,$B4 < TODAY()),""ONE WEEK OLD"",IF(AND($B4 < TODAY() -7,$B4 > TODAY() - 30),""ONE WEEK TO ONE MONTH OLD"",IF(AND($B4 < TODAY()-30,$B4 > TODAY()-90),""1 MONTH TO 90 DAYS OLD"",""OLDER THAN 90 DAYS"")))"

If this doesn't help you'll need to post more of the code.

Upvotes: 3

Related Questions