Autofill to bottom of worksheet (macro not working)

gomes123

New Member
Joined
Jun 16, 2021
Messages
21
Office Version
  1. 2007
Platform
  1. Windows
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!

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
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,216,761
Messages
6,132,569
Members
449,736
Latest member
anthx

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