Reputation: 163
I have the below formula within Excel which works well. This formula is then dragged down to the bottom of the table.
=IF(OFFSET(OriginalData!$A$1,MATCH(TempTable!$A2,OriginalData!$A:$A,0)-1,2,1,1)=0,"",OFFSET(OriginalData!$A$1,MATCH(TempTable!$A2,OriginalData!$A:$A,0)-1,2,1,1))
I now want to add this to vba and have attempted the following:
Private Sub MatchViaEmployeeName()
With Worksheets("TempTable")
Set Usdrws = .Cells(.Rows.Count, 1).End(xlUp)
.Range(.Cells(1, 1), Usdrws.Offset(, 1)).FormulaR1C1 = "=IF(OFFSET(OriginalData!R1C1,MATCH(TempTable!RC[-1],OriginalData!$A:$A,0)-1,2,1,1)=0,"""",OFFSET(OriginalData!R1C1,MATCH(TempTable!RC[-1],OriginalData!$A:$A,0)-1,2,1,1))"
End With
End Sub
My logic is to select the entire range using the 'Usdrws' variable and then entering the formula.
I'm currently getting a
run-time error 1004
Upvotes: 0
Views: 102
Reputation: 7152
In formula you need to change:
""
to """"
in formula$A$1
to R1C1
A:A
to C1
Upvotes: 1
Reputation: 4714
you have to escape the speech marks in your formula because the whole thing is in speechmarks in VBA
.Range(.Cells(1, 1), Usdrws.Offset(, 1)).FormulaR1C1 = _
"=IF(OFFSET(OriginalData!$A$1,MATCH(TempTable!RC[-1],OriginalData!$A:$A,0)-1,2,1,1)=0,"""",OFFSET(OriginalData!$A$1,MATCH(TempTable!RC[-1],OriginalData!$A:$A,0)-1,2,1,1))"
Upvotes: 1