[英]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.