简体   繁体   English

将所有数据从多张纸复制到一张新纸上

[英]Copy all data from multiple sheets into one new sheet

Here is my situation: I want to copy tables from multiple excel sheets and combine it into one new sheet.The macro I have thus far does select the tables, and does create a new sheet to combine the data, HOWEVER it does not select the last row of the tables when combining. 这是我的情况:我想从多个Excel工作表中复制表格并将其合并为一个新工作表。到目前为止,我所拥有的宏确实选择了表格,并创建了一个新工作表来合并数据,但是它没有选择合并时表格的最后一行。 Thanks for the help: 谢谢您的帮助:

 Sub Trytocombine()

 Dim J As Integer


On Error Resume Next
Sheets(1).Select
Worksheets.Add ' add a sheet in first place
Sheets(1).Name = "For Drafting"

' copy headings
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.CurrentRegion.Select
Selection.Copy Destination:=Sheets(1).Range("A1")

' work through sheets
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
    Sheets(J).Activate ' make the sheet active
    Range("A1").Select
    Selection.CurrentRegion.Select ' select all cells in this sheets

    ' select all lines except title
    Selection.Offset(0, 0).Resize(Selection.Rows.Count - 1).Select

    ' copy cells selected in the new sheet on last line
    Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
End Sub

Refactored to avoid select (and copy after last row): 重构以避免选择 (并在最后一行之后复制):

Sub Combine()

Worksheets.Add ' add a sheet in first place
Sheets(1).Name = "For Drafting"

' copy headings
Sheets(1).Range("A1").EntireRow.Value = Sheets(2).Range("A1").EntireRow.Value 'not most effecient, but will do for this

' work through sheets
Dim J As Integer
For J = 2 To Sheets.Count ' from sheet 2 to last sheet
    With Sheets(J)
        .Range(.Cells(2,1),.Cells(.Range("A" & .Rows.Count).End(xlUp).Row,.Cells(2,.Columns.Count).End(xlToLeft).Column)).Copy _ 
             Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(2)
    End With
Next

End Sub

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

相关问题 将多张纸(但不是全部纸)的值复制/粘贴到一张纸中 - Copy/paste values from multiple sheets, but not all sheets, into one sheet 将多张纸中的行复制到一张纸中 - Copy rows from multiple sheets into one sheet 将值从多张纸复制到一张纸 - Copy values from multiple sheets to one sheet VBA 将多张纸从一个 wb 复制到新的 wb(每张纸一个 wb) - VBA to copy multiple sheets from one wb into new wb's (one wb for each sheet) 尝试一张一张地从多张纸复制数据并将其粘贴到另一张纸。 - Trying to copy data from multiple sheets one by one and paste to a different sheet. VBA将列名从一张纸复制到所有其他纸 - VBA Copy column name from one sheet to all other sheets 将数据从多个工作簿的工作表(名称包含“ SAP”)复制到一个工作表中 - Copy data from multiple workbook's sheets (name contains “SAP”) into one sheet 将数据从两个时间范围之间的一张工作表复制到其他多张工作表 - Copy data from one sheet that lies between two time frames, to multiple other sheets 将多个工作表中的列数据复制到同一列中的一个工作表,不会覆盖 - Copy column data from multiple sheets to one sheet in the same column, no overwriting 将数据从多个工作表复制到新工作簿中的多个工作表中 - Copy data from multiple sheets into multiple sheets in new workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM