Excel 代码在模块中工作,但不在工作表中错误1004
希望在下面这段代码上得到帮助。它在模块1中起作用,但在任何工作表中都不起作用。我用有限的知识尽了最大努力,但没能解决它Excel 代码在模块中工作,但不在工作表中错误1004,excel,runtime-error,autofill,vba,Excel,Runtime Error,Autofill,Vba,希望在下面这段代码上得到帮助。它在模块1中起作用,但在任何工作表中都不起作用。我用有限的知识尽了最大努力,但没能解决它 Sub lastrow() Dim MCFPSheet As Worksheet Set MCFPSheet = ThisWorkbook.Worksheets("MCFP Referrals YTD") Dim lastrow As Long lastrow = MCFPSheet.Range("I2").End(xlDown).Row
Sub lastrow()
Dim MCFPSheet As Worksheet
Set MCFPSheet = ThisWorkbook.Worksheets("MCFP Referrals YTD")
Dim lastrow As Long
lastrow = MCFPSheet.Range("I2").End(xlDown).Row
With MCFPSheet.Range("R8")
.AutoFill Destination:=Range("R8:R" & lastrow&)
End With
With MCFPSheet.Range("S2")
.AutoFill Destination:=Range("S2:S" & lastrow&)
End With
With MCFPSheet.Range("T2")
.AutoFill Destination:=Range("T2:T" & lastrow&)
End With
With MCFPSheet.Range("U2")
.AutoFill Destination:=Range("U2:U" & lastrow&)
End With
With MCFPSheet.Range("V2")
.AutoFill Destination:=Range("V2:V" & lastrow&)
End With
With MCFPSheet.Range("W2")
.AutoFill Destination:=Range("W2:W" & lastrow&)
End With
End Sub
我收到
错误1004
在
.AutoFill Destination:=Range(“R8:R”&lastrow&)
行中。根据@sixthense注释,您需要为您的目的地指定工作表,而且如果这些语句都是一行,那么它们也没有多大意义
这样使用和会短得多:
Sub lastrow()
Dim MCFPSheet As Worksheet
Set MCFPSheet = ThisWorkbook.Worksheets("MCFP Referrals YTD")
Dim lastrow As Long
lastrow = MCFPSheet.Range("I2").End(xlDown).Row
With MCFPSheet
.Range("R8").AutoFill Destination:=.Range("R8:R" & lastrow)
.Range("S2").AutoFill Destination:=.Range("S2:S" & lastrow)
.Range("T2").AutoFill Destination:=.Range("T2:T" & lastrow)
.Range("U2").AutoFill Destination:=.Range("U2:U" & lastrow)
.Range("V2").AutoFill Destination:=.Range("V2:V" & lastrow)
.Range("W2").AutoFill Destination:=.Range("W2:W" & lastrow)
End With
End Sub
请注意,Destination:=.Range
现在还使用Range
之前的前导
引用MCFPSheet
表
我还从lastrow&
中删除了&
,我看不出有任何意义。因为您的自动填充目标缺少工作表引用。因此,将“Destination:=”替换为Destination:=MCFPSheet.Ahh,非常感谢!