Help entering R1C1 formula using VBA

garyalan59

New Member
Joined
Dec 18, 2016
Messages
4
Using Excel 2007 and VBA. I need to enter formulas into multiple cells of a worksheet referencing cells on another worksheet in the same workbook. The cells in the source worksheet are not contiguous, so copy/paste an existing formula on the destination worksheet will not work. I recorded a macro as a starting point, but when I alter the macro to loop through all the existing cells where I need the formula, the code throws a "Run-time error '1004': Application-defined or object-defined error."

The functional line in the recorded macro looks like this:
ActiveCell.FormulaR1C1 = "=IF('Time Cards'!R[6]C[-6]="""","""",IF('Time Cards'!R[6]C[-6]=""N"",""N"",'Time Cards'!R[6]C))"

The altered code is as follows:

Sub EnterFormula()
i = 6
j = -6
[FONT=&quot] [/FONT]k = 1
[FONT=&quot] [/FONT]l = 1

[FONT=&quot] [/FONT]For k = 5 To 24
[FONT=&quot] [/FONT][FONT=&quot] [/FONT]For l = 9 To 25
[FONT=&quot] [/FONT][FONT=&quot] [/FONT][FONT=&quot] [/FONT]Cells(k, l).Activate
[FONT=&quot] [/FONT][FONT=&quot] [/FONT][FONT=&quot] [/FONT]ActiveCell.FormulaR1C1 = _
[FONT=&quot] [/FONT][FONT=&quot] [/FONT][FONT=&quot] [/FONT][FONT=&quot] [/FONT]"=IF('Time Cards'!RC[j]="""","""",IF('Time Cards'!RC[j]=""N"",""N"",'Time Cards'!RC[j+6]))"
[FONT=&quot] [/FONT][FONT=&quot] [/FONT][FONT=&quot] [/FONT]i = i + 17
[FONT=&quot] [/FONT][FONT=&quot] [/FONT]Next
[FONT=&quot] [/FONT][FONT=&quot] [/FONT]i = 6
[FONT=&quot] [/FONT][FONT=&quot] [/FONT]j = j + 8
[FONT=&quot] [/FONT]Next
End Sub


it is the ActiveCell.FormulaR1C1 line that throws the error.

Any assistance is most welcome. Otherwise, I'll be entering formulas one at a time for a week...
 

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.
it looks like you are trying to have the vba insert a formula with a quotation mark ("). What you need to do is & """" &. and example of what you need to do is =" &""""& N &""""& ","
 
Upvote 0
Hi

You need to move the variable references (i and j) outside of the string something like this:

ActiveCell.FormulaR1C1 = _
"=IF('Time Cards'!R[" & i & "]C[" & j & "]="""","""",IF('Time Cards'!R[" & i & "]C[" & j & "]=""N"",""N"",'Time Cards'!R[" & i & "]C[" & j + 6 & "]))"

Your previous code is putting the literal value "i" rather than a number which is what it needs.

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,223,719
Messages
6,174,087
Members
452,542
Latest member
Bricklin

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