Excel VBA“范围类的自动填充方法失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1528800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA "Autofill Method of Range Class Failed"
提问by RBarryYoung
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?
以下 VBA 代码 (Excel 2007) 失败,出现错误 1004,“范围类的自动填充方法失败。”。谁能告诉我如何修复它?
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.
(注意:我为此使用了谷歌等。它经常出现,但我看到的所有响应都与格式错误的范围地址有关,AFAIK 不是我的问题。
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.
这会导致我的 Excel 会话进入一个明显的无限循环,消耗 100% CPU,然后永远挂起。
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.
最后一行出现同样的错误。
回答by barrowc
From MSDN:
从MSDN:
The destination must include the source range.
目标必须包括源范围。
B:Udoes not contain A6and thus there is an error. I believe that you probably want outto be set to A6:U6.
B:U不包含A6,因此存在错误。我相信您可能希望out被设置为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:
继下面的 OP 评论并更新到原始答案之后,这可能会奏效:
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
AutoFillis 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
AutoFill一次被限制在一个方向(即水平或垂直)。要从单个单元格填充二维区域,您首先必须沿着该区域的一个边缘自动填充一条线,然后在该区域上拉伸该线
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
回答by MIHIR PATEL
To make AutoFillwork, you need to make the range of AutoFillmore 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
要使AutoFill工作,您需要使范围AutoFill大于源范围。如果自动填充范围与源范围相同,则该范围内没有任何可自动填充的内容,因此您会收到错误消息
1004: AutoFill method of Range class failed.
1004: Range 类的 AutoFill 方法失败。
So make AutoFill range more than the source range and error will gone.
因此,使 AutoFill 范围大于源范围,错误就会消失。
回答by Dom
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?
这有帮助吗?
回答by Richard Hilder
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.
在我的示例中,我必须自动生成从 OA100 到 OA###? 的文件夹名称列表,这很好用。

