Excel VBA “Autofill Method of Range Class Failed”

calendar_today Asked Oct 7, 2009
thumb_up 16 upvotes
history Updated April 16, 2026

Question posted 2009 · +6 upvotes

The following VBA code (Excel 2007) is failing with Error 1004, “Autofill Method of Range Class Failed.”. Can anyone tell me how to fix it?

Dim src As Range, out As Range, wks As Worksheet

Set wks = Me
Set out = wks.Range("B:U")
Set src = wks.Range("A6")
src.AutoFill Destination:=out

(note: I have Googled, etc. for this. It comes up fairly often, but all of the responses that I saw had to do with malformed range addresses, which AFAIK is not my problem.


At someone’s suggestion I tried replacing the autofill line with the following:

src.Copy out

This had the effect of throwing my Excel session into an apparent infinite loop consuming 100% CPU and then just hanging forever.


OK, apparently the source has to be part of the destination range for autofill. So my code now looks like this:

Dim src As Range, out As Range, wks As Worksheet

Set wks = Me
Set out = wks.Range("B1")
Set src = wks.Range("A6")
src.Copy out

Set out = wks.Range("B:U")
Set src = wks.Range("B1")
src.AutoFill Destination:=out, Type:=xlFillCopy

Same error on the last line.

Accepted answer +16 upvotes

From MSDN:

The destination must include the source range.

B:U does not contain A6 and thus there is an error. I believe that you probably want out to be set to A6:U6.

Specifiying just the column name means that you want to fill every row in that column which is unlikely to be the desired behvaiour


Update

Further to the OP’s comment below and update to the original answer, this might do the trick:

Dim src As Range, out As Range, wks As Worksheet

Set wks = Me
Set out = wks.Range("B1")
Set src = wks.Range("A6")
src.Copy out

Set out = wks.Range("B1:U1")
Set src = wks.Range("B1")
src.AutoFill Destination:=out, Type:=xlFillCopy

Set out = wks.Range("B:U")
Set src = wks.Range("B1:U1")
src.AutoFill Destination:=out, Type:=xlFillCopy

AutoFill is constrained to a single direction (i.e. horizontal or vertical) at once. To fill a two-dimensional area from a single cell you first have to auto-fill a line along one edge of that area and then stretch that line across the area

For the specific case of copying the formatting and clearing the contents (by virtue of the source cell being empty), this is better:

Dim src As Range, out As Range, wks As Worksheet

Set wks = Sheet1
Set out = wks.Range("B:U")
Set src = wks.Range("A6")
src.Copy out

2 code variants in this answer

  • Variant 1 — 14 lines, starts with Dim src As Range, out As Range, wks As Worksheet
  • Variant 2 — 6 lines, starts with Dim src As Range, out As Range, wks As Worksheet

Excel VBA objects referenced (4)

  • Range — Refer to Cells by Using a Range Object
  • Range — Delete Duplicate Entries in a Range
  • Worksheet — Refer to All the Cells on the Worksheet
  • Worksheet — List of worksheet functions available to Visual Basic

Top excel-vba Q&A (6)

+16 upvotes ranks this answer #27 out of 136 excel-vba solutions on this site — top 20%.