将行(不是整行)从一张纸复制到另一张纸

时间:2018-12-16 13:45:33

标签: excel vba excel-vba

我在使用以下代码时遇到麻烦。 “后端”是源工作表,“可用性”是目标工作表。任何帮助表示赞赏。

Sub CopyA()

Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("Backend").Cells(Rows.Count, "AB").End(xlUp).Row
lr2 = Sheets("Availability").Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("Backend!AB" & r).Value = "A" Then
           Range("Availability!A" & lr2 + 1 & ":C" & lr2 + 1) = 
           Range("Backend!V" & r & ":X" & r).Value2
           lr2 = Sheets("Availability").Cells(Rows.Count, "A").End(xlUp).Row
        End If
    Next r
End Sub

1 个答案:

答案 0 :(得分:0)

根据您的代码,我认为如果列V:X = Backend,则试图从工作表AB复制A列并将数据粘贴到工作表{的A列中{1}}。

此代码可实现以下目标:

Availability

在您的原始代码中,您使用Sub CopyData() Dim lastRow As Long, rw As Long lastRow = Sheets("Backend").Cells(Rows.Count, "AB").End(xlUp).Row With Worksheets("Backend") For rw = lastRow To 2 Step -1 If .Range("AB" & rw) = "A" Then pasteRow = Worksheets("Availability").Cells(Rows.Count, "A").End(xlUp).Row + 1 Range("V" & rw & ":X" & rw).Copy Destination:=Worksheets("Availability").Range("A" & pasteRow & ":C" & pasteRow) End If Next rw End With End Sub 向后循环。其结果是,粘贴到Step -1中的数据将以相反的顺序。如果您希望粘贴的数据以在Availability上找到的顺序显示,请改用以下代码:

backend