What is proper syntax for formula contained in macro?

imimin

Active Member
Joined
May 9, 2006
Messages
404
Greetings!

How would I properly syntax the following for the expression (formula) on line 1 (I now have it between the double quotes)? As it is, I am getting a 1004 error 'Application-defined or object-defined error'.

Code:
Range("C2").Value = "=IF(COUNTIF(Sheet2!$A$2:$A$10000,A2),VLOOKUP(A2,Sheet2!$A$2:$B$10000,2,FALSE),"")"
Range("C2").AutoFill Destination:=Range("C2:C" & Cells(Rows.Count, "A").End(xlUp).Row), Type:=xlFillSeries

Thank you!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Here is one way:

Code:
Range("C2").Formula = "=IF(COUNTIF(Sheet2!$A$2:$A$10000,A2),VLOOKUP(A2,Sheet2!$A$2:$B$10000,2,FALSE)," & Chr(34) & Chr(34) & ")"
 
Upvote 0
Thanks for your quick reply! That did the trick!

What does the " & Chr(34) & Chr(34) & " do???
 
Last edited:
Upvote 0
The problem is that the double-quotes (") are used as text qualifiers in writing the formulas in VBA, but you actually also want literal double-quotes within your formula, so writing the formula in VBA can be a little tricky.

If you double or triple up the double-quotes, you can get it to work. Personally, I find that a bit confusing and tough to read. The ASCII code for double quotes-is number 34. So CHR(34) is the ASCII code characterization for double-quotes. So:
Chr(34) & Chr(34)
returns two double-quotes.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top