简体   繁体   English

复制到最后一行

[英]Copying until last row

I have multiple sheets and I want to copy their data to another workbook on the last empty row of it. 我有多个工作表,我想将其数据复制到它的最后一个空行上的另一个工作簿中。 The data has a random number of rows but a defined number of columns, and I am having trouble copying it (not all the sheet, only the rows that have data) and then pasting it to the last row of the final workbook. 数据的行数是随机的,但列数是定义的,我在复制数据时遇到了麻烦(不是所有工作表,只有包含数据的行),然后将其粘贴到最终工作簿的最后一行。 How should I do it? 我该怎么办? I have no clue at all... 我一点都不知道

Disclaimer: I don't want to use select of activesheet as I will have multiple workbooks open and this has brought me problems. 免责声明:我不想使用选择活动表,因为我将打开多个工作簿,这给我带来了问题。 Also, the data is continues, and in this I mean that there are no empty rows in the middle of it. 另外,数据还在继续,这意味着中间没有空行。 Also, I don't think this will change anything but this code is running inside a loop so it copies all sheets from multiple files. 另外,我认为这不会改变任何东西,但是此代码在循环内运行,因此可以从多个文件复制所有工作表。

Thanks! 谢谢!

col is the column number you're trying to copy col是您要复制的列号

For Each ws in Activeworkbook.worksheets

    lastrow = ws.cells(ws.rows.count, col).end(xlup).row

    'code to copy and paste

Next ws

I asume you have a handle to the worksheets, so you can use this code to copy the data: 我假设您有工作表的句柄,因此可以使用以下代码复制数据:

Public Sub CopyAcross(oSheetFrom As Worksheet, oSheetTo As Worksheet)

    Const DATA_START_ADDRESS As String = "$A$1"

    oSheetFrom.Range(DATA_START_ADDRESS).CurrentRegion.Copy

    oSheetTo.Range("A" & oSheetTo.Rows.Count).End(xlUp).PasteSpecial xlPasteAll

    Application.CutCopyMode = False

End Sub

Assuming that the data to be copied has the same start address on each sheet, which is specified as DATA_START_ADDRESS 假设要复制的数据在每张纸上具有相同的起始地址,指定为DATA_START_ADDRESS

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

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