代码在模块中工作,但不在工作表中错误1004
问题描述:
希望能够在下面的这段代码中获得帮助。它在模块1中工作,但不适用于任何工作表。我用我有限的知识尽力而为,但一直未能解决。代码在模块中工作,但不在工作表中错误1004
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&)
线。
答
根据@Sixthsense的评论,你需要指定你的目的地的工作表,所有这些With
陈述没有多大意义,如果他们都是一行。
它将使用With
这种方式大大缩短:
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
现在也指MCFPSheet
片使用Range
前面有.
!
而且我也从lastrow&
中删除了&
,我没有看到任何意义。
由于您的自动填充目的地缺少图纸参考。所以用Destination:= MCFPSheet替换“Destination:=”。 – Sixthsense
啊,非常感谢! – Sky