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="] [/FONT]k = 1
[FONT="] [/FONT]l = 1
[FONT="] [/FONT]For k = 5 To 24
[FONT="] [/FONT][FONT="] [/FONT]For l = 9 To 25
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]Cells(k, l).Activate
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]ActiveCell.FormulaR1C1 = _
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]"=IF('Time Cards'!RC[j]="""","""",IF('Time Cards'!RC[j]=""N"",""N"",'Time Cards'!RC[j+6]))"
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]i = i + 17
[FONT="] [/FONT][FONT="] [/FONT]Next
[FONT="] [/FONT][FONT="] [/FONT]i = 6
[FONT="] [/FONT][FONT="] [/FONT]j = j + 8
[FONT="] [/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...
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="] [/FONT]k = 1
[FONT="] [/FONT]l = 1
[FONT="] [/FONT]For k = 5 To 24
[FONT="] [/FONT][FONT="] [/FONT]For l = 9 To 25
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]Cells(k, l).Activate
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]ActiveCell.FormulaR1C1 = _
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]"=IF('Time Cards'!RC[j]="""","""",IF('Time Cards'!RC[j]=""N"",""N"",'Time Cards'!RC[j+6]))"
[FONT="] [/FONT][FONT="] [/FONT][FONT="] [/FONT]i = i + 17
[FONT="] [/FONT][FONT="] [/FONT]Next
[FONT="] [/FONT][FONT="] [/FONT]i = 6
[FONT="] [/FONT][FONT="] [/FONT]j = j + 8
[FONT="] [/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...