Excel VBA “Autofill Method of Range Class Failed”

后端 未结 4 851
醉梦人生
醉梦人生 2020-12-16 22:28

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 Ran         


        
相关标签:
4条回答
  • 2020-12-16 22:50

    To make AutoFill work, you need to make the range of AutoFill more than the source range. If the AutoFill range is same as of Source range then there is nothing to AutoFill in that range and hence you would get an error

    1004: AutoFill method of Range class failed.

    So make AutoFill range more than the source range and error will gone.

    0 讨论(0)
  • 2020-12-16 23:05

    If you want to autofill you just do something like...

    Private Sub Autofill()
    
    'Select the cell which has the value you want to autofill
    Range("Q2").Select
    
    'Do an autofill down to the amount of values returned by the update
    Selection.AutoFill Destination:=Range("Q2:Q10")
    
    End Sub
    

    This would autofill down to the specified range.

    Does ths help?

    0 讨论(0)
  • 2020-12-16 23:09

    Not sure if this helps anyone, but I needed something similar. Selecting the cells as destination works;

    dim rowcount as integer
    Sheets("IssueTemplate").Select ' Whatever your sheet is
    rowcount = 0
    rowcount = Application.CountA(Range("A:A"))'get end range
    Cells(4, 3).Select 'select the start cell
    'autofill to rowcount
    Selection.AutoFill Destination:=Range("C4:C" & rowcount), Type:=xlFillDefault 
    

    in my example I had to auto-generate a list of folder names from OA100 to OA###?, and this worked fine.

    0 讨论(0)
  • 2020-12-16 23:17

    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
    
    0 讨论(0)
提交回复
热议问题