2017-08-18 61 views
1

希望能够在下面的这段代码中获得帮助。它在模块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&)线。

+1

由于您的自动填充目的地缺少图纸参考。所以用Destination:= MCFPSheet替换“Destination:=”。 – Sixthsense

+0

啊,非常感谢! – Sky

回答

1

根据@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&中删除了&,我没有看到任何意义。

相关问题