0
votes

I am trying to paste data from different workbooks into 1 master workbook. So far the copy and pasting of data is working, but, when i paste the data into the workbook, there are rows being skipped after each workbook is being pasted into the master workbook. The picture below shows the problem.problem

2,3 and 6-12 are being skipped. Below is my code:

    Sub Macro1()
'
' Macro1 Macro
'
    Dim wb1 As Workbook
    Set wb1 = ThisWorkbook

Path = "C:\Users\Tester\Documents\test\"
Filename = Dir(Path & "*.xls")
  Do While Filename <> ""
  Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
     For Each Sheet In ActiveWorkbook.Sheets
     Sheet.Rows("2:" & Range("A1").End(xlDown).Row).copy _
        wb1.Sheets(1).Range("A" & Range("A1").End(xlDown).Row + 1)
    Application.CutCopyMode = False
  Next Sheet
     Workbooks(Filename).Close
     Filename = Dir()
  Loop
End Sub

I think the problem has something to do with this line "wb1.Sheets(1).Range("A" & Range("A1").End(xlDown).Row + 1)" but i am not sure how to fix this. Any suggestions? Thank you!

1
See this question for the right way to find the last row.BigBen
Try wb1.Sheets(1).Range("A" & Range("A1").End(xlDown).Row + 1) -> wb1.Sheets(1).Range("A" & wb1.Sheets(1).Range("A1").End(xlDown).Row + 1).ManhND
you are working on open filename so Range("A1").End(xlDown).Row mean last row of open filename in active sheet. You just need to specify for vba that what file you want to find last rowManhND
@ManhND it gave me a Runtime error 1004 application-defined or object-defined error. I specified the file i want to find by setting wb1 which is Thisworkbook, the master workbook.Ruiru
You only define the range you want to copy to. But inside it Range("A1").End(xlDown).Row the file is not specified yet, so excel will get the active file. try to change you destination to wb1.Sheets(1).Range("A" & wb1.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1)ManhND

1 Answers

0
votes

You only define the range you want to copy to, but inside it command Range("A1").End(xlDown).Row the file is not specified yet, so excel will get form active file.

Try to change you destination to

wb1.Sheets(1).Range("A" & wb1.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1)

Your code will look like

Sheet.Rows("2:" & Range("A1").End(xlDown).Row).copy 
     _ wb1.Sheets(1).Range("A" & wb1.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1)