Home » excel » excel – Unable to insert formula from vba

excel – Unable to insert formula from vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

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

How to&Answers:

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.