isaaacs
isaaacs

Reputation: 89

Application-defined error when adding a formula to a cell in VBA

I am trying to add an IF formula to a cell in excel using a macro and it is displaying a 1004 application-defined or object-defined error for the .Range line. How can I prevent this error?

Sub AddFormula()
Dim Formula As Variant
 With ThisWorkbook.Worksheets("Form Responses 1")
    .Range("CE2").Formula = "=IFERROR(IF(OR((BZ2=""No"", BZ2=""0"")), BZ2, AN2), AN2)"

 End With
End Sub

Thanks

Upvotes: 0

Views: 461

Answers (1)

JimmyShoe
JimmyShoe

Reputation: 2259

I think the issue is that the formula you supplied is not valid. There are too many brackets in the OR part.

try this instead

Sub AddFormula()
Dim Formula As Variant
 With ThisWorkbook.Worksheets("Form Responses 1")
.Range("CE2").Formula = "=IFERROR(IF(OR(BZ2=""No"", BZ2=""0""), BZ2, AN2), AN2)"

 End With
End Sub

Upvotes: 0

Related Questions