Multiple Copy and paste from closed workbooks in sequence

SWETHAKHATRI88

New Member
Joined
Jul 29, 2022
Messages
10
Office Version
  1. 2021
Platform
  1. MacOS
I have in total 5 excel workbooks. Workbook 5 has the VBA code. Workbooks 1-4 have the same sheet names but with different content. I do have the below code to import 2 sheets from workbook 1 into workbook 5. Is there any way I can look for the the next blank cell in workbook 5 and import the corresponding data from workbooks 2, 3 and 4.

Final Output should be
Workbook 1 - Copy Sheets 1 and 2 into Workbook 5 (Master Workbook)
Workbook 2 - Copy data from Sheets 1 and 2 from Row 6 only into Workbook 5 next available blank row
Workbook 3 - Copy data from Sheets 1 and 2 from Row 6 only into Workbook 5 next available blank row
Workbook 4 - Copy data from Sheets 1 and 2 from Row 6 only into Workbook 5 next available blank row

My code for workbook 1 is below

Application.ScreenUpdating = False
Set ClosedBook = Workbooks.Open("C:\Status Reports\Workbook1.xlsb")
Closedbook.Sheets("Sheet1").CopyAfter: = ThisWorkbook.Sheets("Control")
Closedbook.Sheets("Sheet2").CopyAfter: = ThisWorkbook.Sheets("Control")
Closedbook.Close SaveChanges: = False

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Are workbooks 1 to 5 the only workbooks saved in "C:\Status Reports\"? Do they all have an "xlsb" extension?
 
Upvote 0
What is the actual name of Workbook1?
 
Upvote 0
(UNTESTED) This macro assumes that the folder contains only the 5 workbooks.
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim desWB As Workbook, desWS As Worksheet, srcWB As Workbook, LastRow As Long, lCol As Long, ws As Worksheet
    Set desWB = ThisWorkbook
    Set desWS = desWB.Sheets("Control")
    Const strPath As String = "C:\Status Reports\"
    Set srcWB = Workbooks.Open("C:\Status Reports\CLDUMP.xlsb")
    Sheets(Array("Sheet1", "Sheet2")).Copy After:=desWB.Sheets(desWB.Sheets.Count)
    srcWB.Close False
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set srcWB = Workbooks.Open(strPath & strExtension)
        If srcWB.Name <> desWB.Name And srcWB.Name <> "CLDUMP.xlsb" Then
            For Each ws In Sheets(Array("Sheet1", "Sheet2"))
                With ws
                    LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    lCol = .Cells(6, .Columns.Count).End(xlToLeft).Column
                    .Range("A" & LastRow).Resize(, lCol).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
                End With
            Next ws
            srcWB.Close False
        End If
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top