0
votes

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.

4
I haven't looked closely enough to see whether it could be related to your issue, but If wb.Name <> "MasterDatabase.xlsx" & "MacrosExcelFile.xls" Then is equivalent to If wb.Name <> "MasterDatabase.xlsxMacrosExcelFile.xls" Then. You want If wb.Name <> "MasterDatabase.xlsx" And wb.Name <> "MacrosExcelFile.xls" ThenYowE3K
I'm guessing your problem will actually be related to the unqualified Rows.Count and Range. Try changing them to include the spreadsheet that you are referring to. (The first one would be Lastrow = 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 unqualified Rows.Count is definitely your issue.YowE3K
This was definitely the problem! I looked at the code a thousand times and couldn't figure that one out, thanks a lot friend!Oz Pere

4 Answers

1
votes
If wb.Name <> "MasterDatabase.xlsx" And wb.Name <> "MacrosExcelFile.xls" Then

Try changing that. I will update this answer when I test it a bit more.

0
votes

I was able to reproduce the problem with the following piece of code

Sub Tester() 
Dim lastrow As Long code here
Dim lastrow As Long
    lastrow = ActiveWorkbook.Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
End Sub

In my case I inserted a chart and it was active when I ran the code. Maybe this helps.

0
votes

I was able to fix "my" issue like that

Sub TestB()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastrow As Long
   Set wkb = ActiveWorkbook
   Set wks = wkb.Worksheets(1)
   lastrow = wks.Cells(wks.Rows.Count, 3).End(xlUp).Row
End Sub
0
votes

SalvadorVayshun is correct

If wb.Name <> "MasterDatabase.xlsx" And wb.Name <> "MacrosExcelFile.xls" Then

Here is how I would refactor the code

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

            With wb.Worksheets(1)
                .Range("B7:J7", .Cells(.Rows.Count, 2).End(xlUp)).Copy
            End With

            With Workbooks("MasterDatabase.xlsx").Worksheets(1)
                .Range("B" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial
            End With

        End If
    Next wb
    Application.ScreenUpdating = True
End Sub

Values only

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

            With wb.Worksheets(1)
                Data = .Range("B7:J7", .Cells(.Rows.Count, 2).End(xlUp)).Value
            End With

            With Workbooks("MasterDatabase.xlsx").Worksheets(1)
                .Range("B" & .Rows.Count).End(xlUp).Offset(1).Resize(UBound(Data, 1), UBound(Data, 2)).Value = Data
            End With

        End If
    Next wb
    Application.ScreenUpdating = True
End Sub

Values and Formulas

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

            With wb.Worksheets(1)
                Data = .Range("B7:J7", .Cells(.Rows.Count, 2).End(xlUp)).Formula
            End With

            With Workbooks("MasterDatabase.xlsx").Worksheets(1)
                .Range("B" & .Rows.Count).End(xlUp).Offset(1).Resize(UBound(Data, 1), UBound(Data, 2)).Formula = Data
            End With

        End If
    Next wb
    Application.ScreenUpdating = True
End Sub