Copy data from a list of workbooks to another master workbook

Viper147

New Member
Joined
Apr 19, 2018
Messages
34
Hi everyone,

Once again I find myself in a bind and need some help please. As mentioned before I am still a noob with VBA so be gentle.
I have a list of workbooks in a range in Excel that I need to copy data from and paste it in a single worksheet (all new data to be appended at the bottom of the previous data set that was copied). I managed to get the bulk of the code right to open the source books and copy the data, but cannot get it right to paste the data in the next empty cell in the target book.
My apologies for the messy code as this has become an abortion of different codes that I have pieced together over the past few days. "a" refers to row nr 1 where the first file location is popuated. The code is then supposed to cycle through the list in column R till it gets to the first empty cell.
The last bit of the code is to compile a list of files that were not found in the relevant folder which will pop up in the end in a messagebox to warn the user of the missing files. As always thanks in advance for the help. See below:

Code:
Sub Test2()
'
' Test2 Macro
'
Dim MyBook As Workbook
Dim OtherBook As Workbook
Dim a As Integer
Dim msg As String
 
a = 1
Set MyBook = ActiveWorkbook
 
Do Until Range("R" & a) = ""
Set OtherBook = Workbooks.Open(Filename:=Range("R" & a))
If OtherBook Is Nothing Then msg = msg & Range("R" & a) Else
OtherBook.Sheets("Data").Range("A2:CN" & Range("Lines")).Copy
MyBook.Sheets("Data").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
OtherBook.Close False
 
a = a + 1
Loop
 
If Len(msg) Then
        MsgBox "Not found" & msg
End If
 
'
End Sub
 
Last edited by a moderator:
Column R contains the full file location whilst column S only contains the file name for the purposes of the msgbox.
 
Upvote 0

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.
Ok, add this line
Code:
Do Until Range("R" & a) = ""
   [COLOR=#ff0000]Set Otherbook = Nothing[/COLOR]
   On Error Resume Next
 
Upvote 0
Added the line of code as you suggested, tested with all the files and ran fine. However when I removed one of the files to test again, it just kept on running without any visible actions performed, same as before. Below is the complete code as it is at the moment.
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Sub Test2()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]'[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]' Test2 Macro[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]'[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim MyBook As Workbook[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim OtherBook As Workbook[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim a As Integer[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim msg As String[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]a = 1[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Set MyBook = ActiveWorkbook[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Do UntilMyBook.Sheets("Sheet1").Range("R" & a) = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Set OtherBook = Nothing[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]On Error Resume Next[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Set OtherBook =Workbooks.Open(Filename:=MyBook.Sheets("Sheet1").Range("R"& a))[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   On Error GoTo 0[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   If OtherBook IsNothing Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]      msg = msg &Range("S" & a)[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Else[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]     OtherBook.Sheets("Data").Range("A2:CN" &Range("Lines")).Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]     MyBook.Sheets("Data").Range("A" &Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]     Application.CutCopyMode = False[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]      OtherBook.CloseFalse[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]      a = a + 1[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   End If[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Loop[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]If Len(msg) Then MsgBox "Not found" & msg[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]'[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0
Remove the 1st file in the list from the folder. Then step through the code using F8.
does it go to the line
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]msg = msg &Range("S" & a)[/COLOR][/SIZE][/FONT]
If so press F8 again & hover the mouse over msg, what does it say?
 
Upvote 0
Hi there. Did as you suggested and it populates msg with the file name that was removed. However I ran F8 again through the code and it added the file name again to msg and so on and so on.
 
Upvote 0
Ok, try this
Code:
  If Otherbook Is Nothing Then
      msg = msg & Range("S" & a)
      [COLOR=#ff0000]a = a + 1[/COLOR]
   Else
 
Upvote 0
Hi there. Tried it but didn't work. However I moved the line (a=a+1) to just before Loop and it worked exactly as it should now.
Thank you so much for all your time and help with this. I really appreciate it.
Cheers
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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