2017-08-18 63 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&中刪除了&,我沒有看到任何意義。

相關問題