Place formula in Cell - error 1004

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
193
Thought this was simple, but keep getting errors.

Pasting into N1 works fine. So does G1, but the line pasting into G2 causes a 1004. Any ideas why?

I also tried using FormulaR1C1 and assigning the formula (=IF(A2=$N$1,"",IF(G1="","",A2))) to a string variable, then pasting that. All of these error out. Manually typing the formula in works fine.

VBA Code:
Sheets("Data1").Range("N1").Value = "****PERIOD TO DATE****"
Sheets("Data1").Activate
Range("G1").Value = "=IF(A1="","",A1)"
Range("G2").Value = "=IF(A2=$N$1,"",IF(G1="","",A2))"
Range("G2").Copy
Range("G3:G1000").PasteSpecial xlPasteFormulas
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You need to double-up the double-quotes, as they are also text qualifiers in VBA.
So this line:
VBA Code:
Range("G2").Value = "=IF(A2=$N$1,"",IF(G1="","",A2))"
need to look like:
VBA Code:
Range("G2").Value = "=IF(A2=$N$1,"""",IF(G1="","",A2))"

Also, while that works, when entering formulas, it is more correct to do:
Rich (BB code):
Range("G2").Formula = "=IF(A2=$N$1,"""",IF(G1="","",A2))"
 
Upvote 0
You need to double-up the double-quotes, as they are also text qualifiers in VBA.
So this line:
VBA Code:
Range("G2").Value = "=IF(A2=$N$1,"",IF(G1="","",A2))"
need to look like:
VBA Code:
Range("G2").Value = "=IF(A2=$N$1,"""",IF(G1="","",A2))"

Also, while that works, when entering formulas, it is more correct to do:
Rich (BB code):
Range("G2").Formula = "=IF(A2=$N$1,"""",IF(G1="","",A2))"
Thanks so much Joe4! I completely brain farted the quad quotes.
 
Upvote 0
You can use the formula in the entire range you want:

Change your code to the following:
VBA Code:
Sub formula_1()
  With Sheets("Data1")
    .Range("N1").Value = "****PERIOD TO DATE****"
    .Range("G1").Value = "=IF(A1="","",A1)"
    .Range("G2:G1000").Formula = "=IF(A2=$N$1,"""",IF(G1="","",A2))"
  End With
End Sub
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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