Copy and Past the Data from Multiple Closed Workbooks

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
119
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi there!
I am seeking help on the following matter: -

I have multiple workbooks, with 05 worksheets, in folder “NewFolder” on the desktop.
The format of Sheets is same in all workbooks.
I after a VBA code which would do the following tasks in a separate workbook called Summery.xlsm

Task 1
Copy all data of Sheet D from Workbook1->and paste it to Sheet1of Summery
Copy all data of Sheet D from Workbook 2->and paste it to Sheet1of Summery
Copy all data of Sheet D from Workbook 3->and paste it to Sheet1of Summery
And so on till last workbook

Task2
Copy all data of Sheet A from Workbook1->and paste it to Sheet2of Summery
Copy all data of Sheet A from Workbook2->and paste it to Sheet2of Summery
Copy all data of Sheet A from Workbook3->and paste it to Sheet2of Summery
And so on till last workbook

Any help on the matter will be highly appreciated.

Links of the workbooks are given below, please
Summery
Book1
Book2
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
try this, i think this will work:
VBA Code:
Sub MergeData()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim fso As Object, fi As Object, fo As Object
    Dim foPath As String
    Dim wb As Workbook, ws As Worksheet
    Dim i As Long, j As Long
    Set folder = Application.FileDialog(msoFileDialogFolderPicker) 'choose target folder
    If folder.Show <> -1 Then Exit Sub
    foPath = folder.SelectedItems(1)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fo = fso.GetFolder(foPath)
    For Each fi In fo.Files
        If fso.GetExtensioNname(fi) Like "xls*" Then 'Loop through files in folder and find excel file
            Set wb = Workbooks.Open(fso.GetAbsolutePathName(fi))
            For Each ws In wb.Sheets 'loop through sheet in workbook and find sheet A and sheet D
                If ws.Name = "A" Then
                    If Not IsEmpty(ws.Range("B3")) Then
                        ws.Range("A3:O" & lr(ws, 1)).Copy Destination:=ThisWorkbook.Sheets(2).Range("A" & lr(ThisWorkbook.Sheets(2), 1) + 1)
                    End If
                ElseIf ws.Name = "D" Then
                    If Not IsEmpty(ws.Range("A6")) Or Not IsEmpty("J6") Then
                        If lr(ws, 2) > lr(ws, 10) Then
                            i = lr(ws, 2)
                        Else
                            i = lr(ws, 10)
                        End If
                        If lr(ws, 1) > lr(ws, 9) Then
                            j = lr(ws, 1)
                        Else
                            j = lr(ws, 9)
                        End If
                        ws.Range("B6:Q" & i).Copy Destination:=ThisWorkbook.Sheets(1).Range("A" & j + 1)
                    End If
                End If
            Next ws
            wb.Close (False)
        End If
    Next fi
    MsgBox "Completed"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Private Function lr(ByVal ws As Worksheet, ByVal col As Integer) As Long
    lr = ws.Cells(Rows.Count, col).End(xlUp).Row
End Function
 
Upvote 0
Thanks for your input, I not familiar with VB Code. i copied your code and paste it in the Module of workbook named summery. But I am facing following error. Please help me on the following error.
 

Attachments

  • Error.png
    Error.png
    71.6 KB · Views: 24
Upvote 0
Thanks for your input, I not familiar with VB Code. i copied your code and paste it in the Module of workbook named summery. But I am facing following error. Please help me on the following error.
show me dialog of error, oh, i see that your orginal summery workbook has 1 sheet, in that case, you should add sheet 2
 
Upvote 0
show me dialog of error, oh, i see that your orginal summery workbook has 1 sheet, in that case, you should add sheet 2
I have added the sheet2 before runing your codes. I noticed that when i run the code. It ask me to select the folder. I select the folder but it doesn't show me the excel files. Please see the picture below:-
 

Attachments

  • Pic.png
    Pic.png
    30.6 KB · Views: 21
Upvote 0
I have added the sheet2. I noticed that when i run the code. It ask me to select the folder. I select the folder but it doesn't show me the excel files. Please see the picture below:-
in this code you don't need to select file, just select folder that include your files
 
Upvote 0
Sorry for my dumb follow up question. I copied your code and paste it in the workbook named summery. Then i selected the Book1 and Book2. Then i came to workbook Summery and run the macro. Am i doing right?
I have already added the sheet2 in summery
 
Upvote 0
Sorry for my dumb follow up question. I copied your code and paste it in the workbook named summery. Then i selected the Book1 and Book2. Then i came to workbook Summery and run the macro. Am i doing right?
I have already added the sheet2 in summery
no, you just need to paste code to your summery workbook and make sure it have 2 sheet or more, then run "MergeData" macro and select folder that included all your workbooks that need to get data from and press OK and wait still done. oh and i has some mistake in code that will find wrong lastrow in sheet 2 of Summery workbook so please change "MergeData" like this:
VBA Code:
Sub MergeData()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim fso As Object, fi As Object, fo As Object
    Dim foPath As String
    Dim wb As Workbook, ws As Worksheet
    Dim i As Long, j As Long
    Set folder = Application.FileDialog(msoFileDialogFolderPicker) 'choose target folder
    If folder.Show <> -1 Then Exit Sub
    foPath = folder.SelectedItems(1)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fo = fso.GetFolder(foPath)
    For Each fi In fo.Files
        If fso.GetExtensioNname(fi) Like "xls*" Then 'Loop through files in folder and find excel file
            Set wb = Workbooks.Open(fso.GetAbsolutePathName(fi))
            For Each ws In wb.Sheets 'loop through sheet in workbook and find sheet A and sheet D
                If ws.Name = "A" Then
                    If Not IsEmpty(ws.Range("B3")) Then
                        ws.Range("A3:O" & lr(ws, 1)).Copy Destination:=ThisWorkbook.Sheets(2).Range("A" & lr(ThisWorkbook.Sheets(2), 1) + 1)
                    End If
                ElseIf ws.Name = "D" Then
                    If Not IsEmpty(ws.Range("A6")) Or Not IsEmpty("J6") Then
                        If lr(ws, 2) > lr(ws, 10) Then
                            i = lr(ws, 2)
                        Else
                            i = lr(ws, 10)
                        End If
                        If lr(ThisWorkbook.Sheets(2), 1) > lr(ThisWorkbook.Sheets(1), 9) Then
                            j = lr(ThisWorkbook.Sheets(1), 1)
                        Else
                            j = lr(ThisWorkbook.Sheets(1), 9)
                        End If
                        ws.Range("B6:Q" & i).Copy Destination:=ThisWorkbook.Sheets(1).Range("A" & j + 1)
                    End If
                End If
            Next ws
            wb.Close (False)
        End If
    Next fi
    MsgBox "Completed"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
i ran this with your files and it worked.
 
Upvote 0
I followed your instructions. The codes doesn't show any error. But nothing happened. Just a blank workbook opened. The picture of which is as under
 

Attachments

  • Untitled1.png
    Untitled1.png
    65 KB · Views: 26
Upvote 0
here is the link for the folder. May be i doing something wrong and couldn't fix it myself.
NewFolder
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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