VBA将数据从一张纸复制到另一张(到空白单元格)

时间:2017-10-10 14:13:50

标签: excel vba excel-vba loops

我想将工作表INV_LEDGERS中的数据复制到Ready to upload工作表中,但工作表Ready to upload已包含一些数据,因此我想在{{1}中遍历A列找到空白单元格,然后粘贴来自Ready to upload的数据。

INV_LEDGERS

它不再显示错误消息,但现在它从行 Sub CopyLedgers() Dim ws As Worksheet, ws1 As Worksheet Dim LastRow As Long Set ws = Sheets("INV_LEDGERS") Set ws1 = Sheets("Ready to upload") LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row For i = 4 To LastRow If ws.Range("A" & i) > "" And ws1.Range("A" & i + 1) = "" Then ws.Range("A" & i & ":AE" & i).Copy ws1.Range("A" & i + 1).PasteSpecial xlPasteValues Application.CutCopyMode = False Else End If Next End Sub 复制数据,其中表INV_LEDGERS上的数据结束。我的意思是,如果Ready to upload上的数据在第82行结束,代码将从82.行中获取Ready to upload的数据,所以基本上缺少81行。

你可以告诉我吗?

非常感谢!

2 个答案:

答案 0 :(得分:0)

偶然的事情......使用了With语句,而不是复制/粘贴,我只是制作了ws1.Value = ws.Value:

Sub CopyLedgers()
    Dim ws As Worksheet, ws1 As Worksheet, LastRow As Long
    Set ws = Sheets("INV_LEDGERS")
    Set ws1 = Sheets("Ready to upload")
    With ws
        LastRow = .Cells( .Rows.Count, 1).End(xlUp).Row
        For i = 4 To LastRow
            If .Range("A" & i) > "" And ws1.Range("A" & i + 1) = "" Then
                ws1.Range("A" & i + 1 & ":AE" & i + 1).Value = .Range("A" & i & ":AE" & i).Value
            End If
        Next
    End With
End Sub

修改

Sub CopyLedgers()
    Dim ws As Worksheet, ws1 As Worksheet, LastRow As Long
    Set ws = Sheets("INV_LEDGERS")
    Set ws1 = Sheets("Ready to upload")
    With ws
        LastRow = .Cells( .Rows.Count, 1).End(xlUp).Row
        For i = 4 To LastRow
            If IsEmpty(ws1.Range("A" & i + 1)) Then
                ws1.Range("A" & i + 1 & ":AE" & i + 1).Value = .Range("A" & i & ":AE" & i).Value
            End If
        Next
    End With
End Sub

答案 1 :(得分:0)

鉴于braX的评论,这是我的代码。 由于您总是从分类帐数据的第4行开始,因此您只需复制整个部分,然后将其粘贴到上传表格的最后一行+ 1。

Sub CopyLedgers()

    Dim ws As Worksheet, ws1 As Worksheet
    Dim LastRow, LRow As Long
    Set ws = Sheets("INV_LEDGERS")
    Set ws1 = Sheets("Ready to upload")

    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    LRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

    ws.Range("A4:AE" & LastRow).Copy
    ws1.Range("A" & LRow + 1).PasteSpecial xlPasteValues

    End Sub