简体   繁体   中英

Unable to copy all data in column to another sheet - excel VBA

I am unable to copy all data in a column. I would only want to copy data that does not include the first row, (headers), and paste it to another sheet. My current code gives me a partial result as there are some blanks in between the data.

Please help me out as I am new to VBA. Thanks!

Below are two codes that I have tried. One is through the xldown method and the other is the lastrow . I found that using the lastrow , I am not even able to copy anything at all.

This is the xldown method, which gives me partial data ( wsRawT and wsDetI are my defined worksheets):

wsRawT.Select
    range("AI1").Select
    ActiveCell.Offset(1, 0).range("A1").Select
    range(Selection, Selection.End(xlDown)).Select
    Selection.copy

wsDetI.Select
    range("B1").Select
    ActiveCell.Offset(1, -1).range("B1").Select
    ActiveSheet.Paste

This is the lastrow method, which does not even allow me to copy anything:

    Dim lastRowTD As Long
    lastRowTD = wsRawT.Cells(Rows.Count, "A").End(xlUp).Row

    wsRawT.range("AU2" & lastRowRD).copy
    wsDetI.range("A2").PasteSpecial xlPasteValues
    wsDetS.range("A2").PasteSpecial xlPasteValues

There are only small mistakes in your code, try this:

Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "AU").End(xlUp).Row

wsRawT.Range("AU2:AU" & lastRowTD).Copy
wsDetI.Range("A2").PasteSpecial xlPasteValues

To explain: This code checks how large the dataset in column A of your sheet wsRawT is. Then it copys everything from cell A2 down to the last row with data. Then all the values are pasted to column A of sheet wsDetI . If you don't specifically need the values only, you could also use this simpler version of .Copy :

wsRawT.Range("AU2:AU" & lastRowTD).Copy wsDetI.Range("A2")

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