简体   繁体   English

VBA-将数据从一张纸复制到另一张纸

[英]VBA - Copy data from one sheet to another

I'm trying to copy data from sheet "DATEV_RAB_UNVERBINDLICH", Range D2:D to sheet "Ready to upload", Range B2:B. 我正在尝试将数据从表“ DATEV_RAB_UNVERBINDLICH”,范围D2:D复制到表“准备上传”,范围B2:B。

I would like find data to the last row and then copy them into the other sheet. 我想找到数据到最后一行,然后将它们复制到另一张纸上。 I have this code: 我有以下代码:

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

But I receive error msg Invalid or unqualified reference . 但是我收到错误消息msg 无效或不合格的引用 Could you advise me, what do I do wrong, please? 您能告诉我,我做错了什么吗?

Two errors in your code 您的代码中有两个错误

At line 3, this is not the correct way to define multiple variable types in the same line. 在第3行,这不是在同一行中定义多个变量类型的正确方法。 You have to define each of them. 您必须定义每个。 At line 10, lets say your lastrow is "20". 在第10行,假设您的最后一行是“ 20”。 It would set the range to "D220", which is also not what you want. 它将范围设置为“ 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

I also changed to define the variables and its values at the start of the code. 我还更改了代码的开头,以定义变量及其值。 The lastrow bit didn't code here too after a bit of testing. 经过一些测试,lastrow位在这里也没有编码。 Now it works for me at least. 现在至少对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM