When working with Excel VBA to fill formulas or data automatically, you might run into the error: “AutoFill method of Range class failed.” This error typically occurs when your macro tries to apply AutoFill to an invalid or misaligned range.
In this article, you’ll learn three practical ways to fix the AutoFill error in Excel VBA. Each solution targets a common cause, from incorrect destination ranges to dynamic referencing issues, so you can identify and resolve the problem effectively.
Steps to fix the “AutoFill method of Range class failed” error by expanding the destination range to include the source and extend down to your data:
➤ Go to your worksheet and make sure your dataset includes at least two numeric columns like Quantity (B) and Unit Price (C), and an empty Total Price column (D)
➤ Press Alt + F11 , choose Insert >> Module, and paste the code below:
Sub AutoFill_Fix_DestinationRange()
Dim ws As Worksheet
Dim lastRow As Long
Dim srcCell As Range
Dim destRange As Range
Set ws = ThisWorkbook.Sheets("Sheet1") ' Source sheet
Set srcCell = ws.Range("D2") ' Cell containing formula
' Insert the formula in D2
srcCell.Formula = "=B2*C2"
' Find the last used row in column B (Quantity)
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
' Define destination range: D2 down to the last row
Set destRange = ws.Range("D2:D" & lastRow)
' Apply AutoFill
srcCell.AutoFill Destination:=destRange, Type:=xlFillDefault
MsgBox "Total Price formula autofilled from D2 to D" & lastRow & ".", vbInformation
End Sub
➤ The macro first writes the formula =B2*C2 in cell D2 to calculate Total Price
➤ It then calculates the last used row in column B and builds a dynamic destination range (e.g., D2:D11)
➤ .AutoFill copies the formula down from D2 to D11 without triggering an error
➤ A message box confirms the autofill was successful
➤ To run the macro, return to Excel, press Alt + F8 , select AutoFill_Fix_DestinationRange, and click Run
➤ You’ll see the Total Price column correctly filled for all product rows without any AutoFill error
Solution 1: Expand the Destination Range to Include the Source Cell
The AutoFill method fails if the destination range does not extend beyond the source cell. In our test macro, the code tries to fill from D2 to D2, which is just a single cell. By expanding the destination to cover all rows in use, AutoFill works without error.
In this fix, we dynamically calculate the last row in column B and set the destination range from D2 down to that last row.
Before we move to the solution, let’s simulate the “AutoFill method of Range class failed” error using a simple dataset:
We want to calculate Total Price in column D using the formula =B2*C2 and apply it to all rows. A common mistake is to try the following VBA code:
Sub TriggerAutoFillError()
Range("D2").Formula = "=B2*C2"
Range("D2").AutoFill Destination:=Range("D2"), Type:=xlFillDefault
End Sub
This macro sets the formula in D2 and then tries to autofill… into just D2. Since the destination range doesn’t extend beyond the source, Excel throws: Run-time error ‘1004’: AutoFill method of Range class failed
To resolve this, we’ll walk through three practical solutions that handle this error using best practices like checking the dataset size, adjusting the destination range, and ensuring there are no empty rows.
Steps:
➤ Press Alt + F11 , choose Insert >> Module.
➤ Paste the revised code below.
Sub AutoFill_Fix_DestinationRange()
Dim ws As Worksheet
Dim lastRow As Long
Dim srcCell As Range
Dim destRange As Range
Set ws = ThisWorkbook.Sheets("Sheet1") ' Source sheet
Set srcCell = ws.Range("D2") ' Cell containing formula
' Insert the formula in D2
srcCell.Formula = "=B2*C2"
' Find the last used row in column B (Quantity)
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
' Define destination range: D2 down to the last row
Set destRange = ws.Range("D2:D" & lastRow)
' Apply AutoFill
srcCell.AutoFill Destination:=destRange, Type:=xlFillDefault
MsgBox "Total Price formula autofilled from D2 to D" & lastRow & ".", vbInformation
End Sub
➧ lastRow finds the bottom of data in column B to set the correct destination length.
➧ destRange = ws.Range("D2:D" & lastRow) ensures the destination includes the source cell and extends far enough.
➧ srcCell.AutoFill fills the formula down the entire destination range.
➧ A message box confirms the operation and indicates the final row.
➤ Return to Excel, press Alt + F8 , select AutoFill_Fix_DestinationRange, and click Run.
➤ Column D now shows Total Price values for every product row without triggering the AutoFill error.
Solution 2: Define Destination Range Dynamically Based on Last Row
This error often happens when the destination range for AutoFill is not tall enough to copy the formula downward. To prevent this, you can calculate the last row dynamically and then construct a proper range to autofill.
In this example, we’ll set the formula in cell D2 and autofill it down to the last used row in column B.
Steps:
➤ Press Alt + F11 , choose Insert >> Module, and paste this code:
Sub AutoFillUsingLastRow()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Find last row in Quantity column
ws.Range("D2").Formula = "=B2*C2" ' Set formula in D2
ws.Range("D2").AutoFill Destination:=ws.Range("D2:D" & lastRow)
MsgBox "Formula applied from D2 to D" & lastRow, vbInformation
End Sub
➧ ws.Range("D2:D" & lastRow) constructs a range from D2 down to the last row.
➧ The macro safely applies the formula without triggering the AutoFill error.
➤ Go back to Excel, press Alt + F8 , choose AutoFillUsingLastRow, and click Run.
➤ Your Total Price column will now be filled correctly for every row with product data.
Solution 3: Use Resize Instead of AutoFill to Avoid Range Class Error
If AutoFill keeps failing, an alternative is to use the Resize method, which directly sets a formula across a target range without needing AutoFill. This bypasses the issue entirely.
In this example, we’ll apply the =B2*C2 formula to the entire Total Price column (D) using Resize, starting from D2 down to the last row in column B.
Steps:
➤ Press Alt + F11 , choose Insert >> Module and paste this code:
Sub FillFormulaWithResize()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Find last row in Quantity column
ws.Range("D2").Resize(lastRow - 1).Formula = "=B2*C2" ' Apply formula using Resize
MsgBox "Formula applied to range D2:D" & lastRow, vbInformation
End Sub
➧ Resize(lastRow - 1) expands from D2 down to match the data length.
➧ This method avoids AutoFill completely while achieving the same result.
➤ Return to Excel, press Alt + F8 , run FillFormulaWithResize, and your formulas will populate instantly.
➤ No AutoFill error, no guesswork, just a clean, direct fill from top to bottom.
Frequently Asked Questions
Why does Excel throw “AutoFill method of Range class failed” when destination equals source?
AutoFill requires the destination range to be larger than the source. If the destination is identical or smaller, Excel can’t extend the formula, causing error 1004.
How do I prevent the error when there’s only one row of data?
If lastRow equals the header row (e.g., row 2), AutoFill may fail. Add a check like If lastRow > 2 Then before calling AutoFill.
The destination doesn’t include the source range, why does this matter?
AutoFill’s destination must span at least the source range. Misaligned ranges cause it to fail; ensure your selection starts at the source, e.g., “D2:D10” covers D2.
Can copying and expanding manually avoid this error?
Yes. Instead of AutoFill, you can copy and use Resize or copy-paste formulas into a target range, bypassing the Range.AutoFill method altogether.
Wrapping Up
The “AutoFill method of Range class failed” error often results from incorrect range definitions, missing data, or mismatched source and destination ranges. By applying the solutions above, checking data length, validating source ranges, handling single rows, or using alternative methods, you can reliably resolve the issue and make your VBA code run smoothly. Always test your ranges and logic before applying AutoFill to avoid runtime errors in large or dynamic datasets.