kit
kit

Reputation: 83

excel formula within vba 1004 error

I am comparing two columns and displaying the differences in another column. I have an excel formula that works but I need to use it in a vba macro. When I try to usie it in the macro though I get the error "Run-time Error: '1004': Application-defined or object-defined error". How can this be fixed?

Sub Macro2()

   Range("B2:B60").Formula = "=IF(COUNTIF($A:$A,$B2)=0, B2, "")"

End Sub

Upvotes: 0

Views: 194

Answers (1)

YowE3K
YowE3K

Reputation: 23994

Quotation marks within a string need to be doubled-up so, instead of

Range("B2:B60").Formula = "=IF(COUNTIF($A:$A,$B2)=0, B2, "")"

use

Range("B2:B60").Formula = "=IF(COUNTIF($A:$A,$B2)=0, B2, """")"

but please note that this will give a circular reference error, as cell B2 (etc) will be referencing itself.

Maybe you are intending to write the new value to column C? If so, use

Range("C2:C60").Formula = "=IF(COUNTIF($A:$A,$B2)=0, B2, """")"

Upvotes: 3

Related Questions