2017-10-16 187 views
0

我想從第二行的工作表「Inv_Headers」,C列中的數據複製數據,直到最後一行從第4行復制到工作表「客戶」列U.VBA - 將可變範圍從一張紙複製到另一張

Private Sub Invoice_C() 
    Dim ws As Worksheet, ws1 As Worksheet 
    Dim lastrow As Long 

    Set ws = Worksheets("Inv_Headers") 
    Set ws2 = Worksheets("CUSTOMERS") 


     lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C 
     ws.Range("C2:C" & lastrow).Copy 
     ws1.Range("U4").PasteSpecial xlPasteValues 
     ws1.Activate 


End Sub 

我的代碼給了我錯誤消息'91' - 對象變量或塊變量未設置。但代碼應該沒有使用聲明,不應該嗎?

請問您的建議嗎?

非常感謝!

+1

你叫你的第一張WS,不WS1,所以更改WS1在:ws1.Range( 「U4」)PasteSpecial的xlPasteValues – Absinthe

+0

@Absinthe哦,該死。謝謝!我沒有注意到。 – Srpic

+1

爲什麼您應該爲每個模塊使用Option Explicit的完美示例,並在運行之前進行編譯。這會立即在'Set ws2 = ....'行上產生一個錯誤 –

回答

0

基於從@Absinthe檢查,我已經改正錯字,這裏是正確的代碼:

Private Sub Invoice_C() 
    Dim ws As Worksheet, ws1 As Worksheet 
    Dim lastrow As Long 

    Set ws = Worksheets("Inv_Headers") 
    Set ws1 = Worksheets("CUSTOMERS") 


     lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C 
     ws.Range("C2:C" & lastrow).Copy 
     ws1.Range("U4").PasteSpecial xlPasteValues 
     ws1.Activate 


End Sub 
0

除了Srpic的產品,我記得沒有得到這部分的工作: ws.Range("C2:C" & lastrow).Copy

您可以如果您在範圍(開始打字ws.Range("C2", "C" & lastrow).Copy

修復它),你會看到,是可接受的分離,而:爲INCOM完整範圍的確定不是。

Private Sub Invoice_C() 
Dim ws As Worksheet, ws1 As Worksheet 
Dim lastrow As Long 

Set ws = Worksheets("Inv_Headers") 
Set ws1 = Worksheets("CUSTOMERS") 


    lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C 
    ws.Range("C2", "C" & lastrow).Copy 
    ws1.Range("U4").PasteSpecial xlPasteValues 
    ws1.Activate 

末次

相關問題