0

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
0

1 Answer 1

3

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, """")"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this just changes the B column to 0's. Do you know of a ways a way around this? In excel the formula works as expected.
@kit - the code just writes the formula into the Excel cell, so if you entered the formula in Excel manually you would also get a circular reference error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.