I have this excel macro below, where the majority of the code "seems fine" until the last row, when the runtime error comes up, stating "Autofill method of range class failed". Seems like the problem in the code is the "fillRange.AutoFill Destination:=fillRange"
How would I fix this? Thanks!
Basically I'm trying to use the macro to type in formulae in the cells C3-H3, and then autofill drag it down until the last row (basically there are data in columns A and B, starting from Cell A2 and Cell B2. It may be up to A1000 and B1000, but tomorrow it would be A1050 and B1050. So that's why I need the autofill dragdown in the macro. Thanks!
How would I fix this? Thanks!
Basically I'm trying to use the macro to type in formulae in the cells C3-H3, and then autofill drag it down until the last row (basically there are data in columns A and B, starting from Cell A2 and Cell B2. It may be up to A1000 and B1000, but tomorrow it would be A1050 and B1050. So that's why I need the autofill dragdown in the macro. Thanks!
VBA Code:
Sub Macro1()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("C3").FormulaR1C1 = "=VLOOKUP(RC[-1],'Day1'!C[-1]:C[1],2,0)"
ws.Range("D3").FormulaR1C1 = "=VLOOKUP(RC[-2],'Day1'!C[-2]:C,3,0)"
ws.Range("E3").FormulaR1C1 = "=VLOOKUP(RC[-3],'Day2'!C[-3]:C[-1],2,0)"
ws.Range("F3").FormulaR1C1 = "=VLOOKUP(RC[-4],'Day2'!C[-4]:C[-2],3,0)"
ws.Range("G3").FormulaR1C1 = "=VLOOKUP(RC[-5],'Day3'!C[-5]:C[-3],2,0)"
ws.Range("H3").FormulaR1C1 = "=VLOOKUP(RC[-6],'Day3'!C[-6]:C[-4],3,0)"
Dim fillRange As Range
Set fillRange = ws.Range("C3:H3").Resize(lastRow - 2)
fillRange.AutoFill Destination:=fillRange
End Sub