0
votes

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. 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
1
I don't usually use "Offset" and "Selection" and similar things, but I'm guessing the "Selection.Offset(0, 0)" should be "Selection.Offset(1, 0)". As it currently is, I think it is copying the first row to the last row - 1. (But Scott's method is a much more preferred way of doing things.)YowE3K
just replace .Offset(0, 0) with .Offset(1, 0)Slai

1 Answers

1
votes

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