Autofill formula via VBA erroring

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
303
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub Email_Mapping()

Dim wsSheet1, wsSheet2, wsSheet3 As Worksheet
Dim LastRow As Long

Application.Calculation = xlCalculationAutomatic
Set wsSheet1 = Worksheets("statements list")
LastRow = wsSheet1.Columns(1).Find("*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row

wsSheet1.Range("B2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],'statements email mapping'!C[-1]:C,2,FALSE),""CHECK"")"
wsSheet1.Range("C2").Value = "Y"
wsSheet1.Range("B2").AutoFill Destination:=Range("B2:B" & LastRow), Type:=xlFillDefault '<<< fails on this line
wsSheet1.Range("C2").AutoFill Destination:=Range("C2:C" & LastRow), Type:=xlFillDefault
wsSheet1.Columns("A:D").Select
wsSheet1.Columns("A:D").EntireColumn.AutoFit
End Sub

If I F8 this it runs perfectly, if I trying running the macro from a button it errors?

What have I missed?

Many thanks as always for your help.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try prefixing the destination with the sheet reference eg:
Rich (BB code):
wsSheet1.Range("B2").AutoFill Destination:=wsSheet1.Range("B2:B" & LastRow), Type:=xlFillDefault
Since you are repeating the sheet reference why not enclose it all in With wsSheet1
VBA Code:
With wsSheet1
   ' use same format for previous and later lines
    .Range("B2").AutoFill Destination:=.Range("B2:B" & LastRow), Type:=xlFillDefault
End with
 
Upvote 1
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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