简体   繁体   中英

VBA copy data from one sheet to another (to the blank cells)

I would like to copy data from sheet INV_LEDGERS into Ready to upload sheet, but the sheet Ready to upload already contains some data and therefore I want to loop through the column A in Ready to upload sheet until it will find the blank cell and then paste the data from 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

It doesnt show the error msg anymore, but now it copies the data from INV_LEDGERS from the row, where data on sheet Ready to upload ends. I mean, that if data on Ready to upload has the end on row 82, the code will take the data from INV_LEDGERS from 82. row, so basically there are missing 81 rows.

Could you advise me, please?

Thanks a lot!

Couple things... have used a With statement, and instead of copy/paste, I'm just making 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

Edit

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

Given the comments from braX, here is my code. Since you are always starting on the 4th row of the ledger data, you can just copy the entire section and then paste it to your last row + 1 on your upload sheet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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