2017-09-05 96 views
0

我試圖將數據從「DATEV_RAB_UNVERBINDLICH」紙張,範圍D2:D複製到「準備上傳」紙張,範圍B2:B。VBA - 將數據從一張紙複製到另一張

我想找到最後一行的數據,然後將它們複製到另一張表。我有這樣的代碼:

Sub CopyInvoiceNo() 

Dim ws, ws1 As Worksheet 
Dim lastrow As Long 

lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row 

    Set ws = Sheets("DATEV_RAB_UNVERBINDLICH") 
    Set ws1 = Sheets("Ready to upload") 
    ws.Range("D2" & lastrow).Copy 
    ws1.Range("B4").PasteSpecial xlPasteValues 
    ws1.Activate 

End Sub 

但我收到錯誤味精無效的或不合格的參考。你能不能告訴我,我該怎麼做錯了?在你的代碼

回答

1

兩個錯誤在第3行,這是不是在同一行中定義多個變量類型的正確方法。你必須定義它們中的每一個。 在第10行,可以說你的lastrow是「20」。它會將範圍設置爲「D220」,這也不是你想要的。

Sub CopyInvoiceNo() 

Dim ws As Worksheet, ws1 As Worksheet 
Dim lastrow As Long 
Set ws = Sheets("DATEV_RAB_UNVERBINDLICH") 
Set ws1 = Sheets("Ready to upload") 

    lastrow = ws.Cells(Rows.count, 4).End(xlUp).Row 
    ws.Range("D2:D" & lastrow).Copy 
    ws1.Range("B4").PasteSpecial xlPasteValues 
    ws1.Activate 

End Sub 

我也改變了定義變量及其在代碼開始時的值。經過一些測試,lastrow位在這裏也沒有編碼。至少現在它適用於我。

+0

嗯,現在我明白了。感謝您的建議!它工作正常。 – Srpic

相關問題