简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM