Excel VBA“范围类的自动填充方法失败”

发布于 2024-08-06 16:41:52 字数 775 浏览 6 评论 0原文

以下 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

(注意:我已经为此进行了谷歌搜索等。它经常出现,但我看到的所有响应都与格式错误的范围地址有关,据我所知这不是我的问题。


根据某人的建议,我尝试替换自动填充行其结果如下:

src.Copy out

这会使我的 Excel 会话陷入明显的无限循环,消耗 100% CPU,然后永远挂起,


显然源必须是自动填充的目标范围的一部分,所以我的代码现在看起来像这样。 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

最后一行出现同样的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秉烛思 2024-08-13 16:41:52

来自 MSDN

目的地必须包含
来源范围。

B:U 不包含 A6,因此存在错误。我相信您可能希望将 out 设置为 A6:U6

仅指定列名称意味着您想要填充该列中的每一行,这不太可能是所需的行为


更新

进一步按照下面的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

自动填充一次被限制在一个方向(即水平或垂直)。要从单个单元格填充二维区域,您首先必须沿着该区域的一个边缘自动填充一条线,然后将该线延伸到该区域

对于复制格式和清除内容的特定情况(通过源单元格为空),这样更好:

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

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
最舍不得你 2024-08-13 16:41:52

要使AutoFill 正常工作,您需要使AutoFill 的范围大于源范围。如果自动填充范围与源范围相同,则该范围内没有任何内容可自动填充,因此您会收到错误

1004: Range 类的 AutoFill 方法失败。

因此,使自动填充范围大于源范围,错误就会消失。

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.

哎呦我呸! 2024-08-13 16:41:52

如果您想自动填充,只需执行以下操作...

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

这将自动填充到指定的范围。

这有帮助吗?

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?

生活了然无味 2024-08-13 16:41:52

不确定这是否对任何人有帮助,但我需要类似的东西。选择单元格作为目标有效;

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 

在我的示例中,我必须自动生成从 OA100 到 OA###? 的文件​​夹名称列表,这工作得很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文