Application or Object Defined Error

Nelly2015

New Member
Joined
Mar 3, 2015
Messages
28
No idea what went wrong. Need help with the following code. It is simple.


Sub Purchase2020()
Dim FinalRow As Long
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveWorkbook.Sheets("2020").Range("AJ2").Select
Range("AJ2:AJ" & FinalRow) = "=IFERROR(RC[-21]/RC[-1],"")"
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
VBA Code:
Range("AJ2:AJ" & FinalRow).FormulaR1C1 = "=IFERROR(RC[-21]/RC[-1],"""")"
 
Upvote 0
Solution
But I don't why you put """" instead of "". Any reason ?
Yes, the formula is a string enclosed by quote marks. If you use standard quote marks within the string, vba does not know if they are part of the string or whether the first one marks the end of the string so you double them up. Once the formula is in the worksheet you will see that those 4 quote marks revert to two.

Rich (BB code):
Range("AJ2:AJ" & FinalRow) = "=IFERROR(RC[-21]/RC[-1],"""")"

BTW, although not mandatory, using .FormulaR1C1 would make it clearer that your code is inserting a formula in that format.
 
Upvote 0
Yes, the formula is a string enclosed by quote marks. If you use standard quote marks within the string, vba does not know if they are part of the string or whether the first one marks the end of the string so you double them up. Once the formula is in the worksheet you will see that those 4 quote marks revert to two.

Rich (BB code):
Range("AJ2:AJ" & FinalRow) = "=IFERROR(RC[-21]/RC[-1],"""")"

BTW, although not mandatory, using .FormulaR1C1 would make it clearer that your code is inserting a formula in that format.
Thanks for your clear and detailed explanations. The code worked after doubling the quotation marks.
 
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,680
Members
449,328
Latest member
easperhe29

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