Autofill formula via VBA erroring

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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