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.
If wb.Name <> "MasterDatabase.xlsx" & "MacrosExcelFile.xls" Then
is equivalent toIf wb.Name <> "MasterDatabase.xlsxMacrosExcelFile.xls" Then
. You wantIf wb.Name <> "MasterDatabase.xlsx" And wb.Name <> "MacrosExcelFile.xls" Then
– YowE3KRows.Count
andRange
. Try changing them to include the spreadsheet that you are referring to. (The first one would beLastrow = wb.Worksheets(1).Cells(wb.Worksheets(1).Rows.Count, 2).End(xlUp).Row
, and that's probably the one casuing the problem, but the later one should be changed too to be safe.) Oops - didn't read your question far enough - that unqualifiedRows.Count
is definitely your issue. – YowE3K