简体   繁体   中英

Fixing vba copy rows data from a column to another sheet

I am trying with this code. There are numerous rows in column 10 in my sheet Concur, but when i run the codes, it only copy paste the last row in my destination. I not sure whats go wrong....

Dim ConcurLastRow As Long
Set Concur = ThisWorkbook.Worksheets("Concur")

ConcurLastRow = Concur.Cells(Rows.Count, 1).End(xlUp).Row

For i = 11 To ConcurLastRow
Worksheets("Concur").Cells(i, 10).Copy

UploadtoSunLastRow = Worksheets("UploadtoSun").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Worksheets("Concur").Paste Destination:=Worksheets("UploadtoSun").Cells(UploadtoSunLastRow, 4)

Next i

You are using Column 1 to get the last row on both sheets but are populating column 4 on worksheet("UploadtoSun") .

You want to change:

UploadtoSunLastRow = Worksheets("UploadtoSun").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

to:

UploadtoSunLastRow = Worksheets("UploadtoSun").Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Row

Now you are comparing the last row with the column you are adding rows to.

Though as pointed out by BigBen if you are grabbing a contiguous range and dumping to another contiguous range you can just copy the entire range over in one shot with no need for a loop.

This would do the same with no loop:

    Dim ConcurLastRow As Long
    Dim UploadtoSunLastRow As Long
    Dim concur As Worksheet
    Set concur = ThisWorkbook.Worksheets("Concur")

    ConcurLastRow = concur.Cells(Rows.Count, 10).End(xlUp).Row
    UploadtoSunLastRow = Worksheets("UploadtoSun").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    With concur
        .Range(.Cells(11, 10), .Cells(ConcurLastRow, 10)).Copy Worksheets("UploadtoSun").Cells(UploadtoSunLastRow, 4)
    End With

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