0
votes

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
1

1 Answers

2
votes

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, """")"