简体   繁体   English

无法将列中的所有数据复制到另一个工作表 - excel VBA

[英]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.请帮助我,因为我是 VBA 的新手。 Thanks!谢谢!

Below are two codes that I have tried.以下是我尝试过的两个代码。 One is through the xldown method and the other is the lastrow .一种是通过xldown方法,另一种是lastrow I found that using the lastrow , I am not even able to copy anything at all.我发现使用lastrow ,我什至无法复制任何东西。

This is the xldown method, which gives me partial data ( wsRawT and wsDetI are my defined worksheets):这是xldown方法,它给了我部分数据( wsRawTwsDetI是我定义的工作表):

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:这是lastrow方法,它甚至不允许我复制任何东西:

    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.解释一下:此代码检查工作表wsRawTA列中的数据集有多大。 Then it copys everything from cell A2 down to the last row with data.然后它将单元格A2中的所有内容复制到包含数据的最后一行。 Then all the values are pasted to column A of sheet wsDetI .然后将所有值粘贴到工作表wsDetI A列。 If you don't specifically need the values only, you could also use this simpler version of .Copy :如果您并不特别需要这些值,您还可以使用这个更简单的.Copy版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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