Sunday, 25 June 2017

vba - Excel - Error when trying to loop through workbooks

Good day friends,
I'm receiving an error whenever I try to loop through all open workbooks in order to copy and paste to a master workbook.
For the life of me I can't figure out how to rectify it, could any of you kindly assist?



Sub LoopCopyPaste()
Dim wb As Workbook
Dim Lastrow As Long
For Each wb In Application.Workbooks
If wb.Name <> "MasterDatabase.xlsx" & "MacrosExcelFile.xls" Then

Lastrow = wb.Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
wb.Worksheets(1).Range("B7:J" & Lastrow).Copy
''
Windows("MasterDatabase.xlsx").Activate
Range("B" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
End If
Next wb
End Sub



The error is "1004, Application-defined or object-defined error", and it points to the "Lastrow = wb.Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row" sentence.
What can I do to solve this issue?
Thanks in advance.

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...