Range.Autofill方法给出错误1004

时间:2014-10-24 09:33:06

标签: vba excel-vba excel

我有一个简单的代码,它不起作用。请给我一个提示是什么问题。因为我不知道... :(
出现的错误是:

运行时错误' 1004'
Range类的自动填充方法失败

调试器突出显示代码的最后一行 请参阅以下代码:
Sub why_u_no_work()
Dim b As Integer
Dim lastrowincurrfund As Integer
Dim fundslistsym(0 To 0) As String
Dim SourceRange As Range
Dim fillRange As Range

lastrowincurrfund = 142
b = 0
fundslistsym(b) = "DU"
Set SourceRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC1")
Set fillRange = Range("P1:AC" & lastrowincurrfund)
SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
End Sub

非常感谢提前

2 个答案:

答案 0 :(得分:1)

问题在于分配填充范围变量: 我已更新它,检查并告诉我:

Sub why_u_no_work()

    Dim b As Integer
    Dim lastrowincurrfund As Integer
    Dim fundslistsym(0 To 0) As String
    Dim SourceRange As Range
    Dim fillRange As Range

    lastrowincurrfund = 142
    b = 0
    fundslistsym(b) = "DU"
    Set SourceRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC1")
    Set fillRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC" & lastrowincurrfund)
    SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault

End Sub

Sub why_u_no_work()

    Dim b As Integer
    Dim lastrowincurrfund As Integer
    Dim fundslistsym(0 To 0) As String
    Dim SourceRange As Range
    Dim fillRange As Range

    lastrowincurrfund = 142
    b = 0
    fundslistsym(b) = "DU"
    With ThisWorkbook.Worksheets(fundslistsym(b))
        Set SourceRange = .Range("P1:AC1")
        Set fillRange = .Range("P1:AC" & lastrowincurrfund)
        SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
    End With

End Sub

答案 1 :(得分:0)

我想我找到了解决办法 如果要使用该方法的工作表未激活,则Range.Autofill方法不起作用。因此,为了克服这个问题,我必须添加:

`ThisWorkbook.Worksheets(fundslistsym(b))。Activete

这就是全部......
我想让代码运行,但我不明白为什么需要激活工作表。如果有人可以帮忙,请告诉我。我非常渴望知道为什么会这样。
非常感谢 阿图尔