VBA to copy data from multiple workbooks into new master workbook

MSpicewood

New Member
Joined
May 15, 2018
Messages
13
Good morning,


I'm a VBA novice and having a hard time with my first major project. I've been researching solutions to copy all the data from multiple files (.XLS?) in a directory called "C:/allfiles/" to a newly created master report file.

I need a solution that won't capture any additional blank rows as each file has a different number of entries. I'm also trying to only pull from the first worksheet on each workbook file. These files have a second tab called LOG which I'm trying to exclude.

I have referenced a previous topic here to get started but it's not working out as my specifications are a bit different. Referenced: https://www.mrexcel.com/forum/excel...ata-multiple-workbooks-into-master-sheet.html

Any help is greatly appreciated. Thank you.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Do you want all the data to go onto 1 sheet?
 
Upvote 0
Do you want to copy each sheet to one sheet in the master report or each sheet to a separate sheet? You want to delete all the blank rows before copying the sheets. Is this correct?
 
Upvote 0
If you want all sheets to go to 1 master sheet, try
Code:
Sub CopyFiles()
   Dim Mws As Worksheet
   Dim Wbk As Workbook
   Dim Fname As String
   Const Pth As String = "C:\AllFiles\"
   
   Set Mws = ThisWorkbook.Sheets("[COLOR=#ff0000]Master[/COLOR]")
Application.ScreenUpdating = False
   Fname = Dir("*.xls*")
   Do While Fname <> ""
      Set Wbk = Workbooks.Open(Pth & Fname)
      Wbk.Sheets(1).Range("A1").CurrentRegion.Copy Mws.Range("A" & Rows.Count).End(xlUp).Offset(1)
      Wbk.Close False
      Fname = Dir
   Loop
End Sub
Change the value in red to suit
 
Upvote 0
Do you want to copy each sheet to one sheet in the master report or each sheet to a separate sheet? You want to delete all the blank rows before copying the sheets. Is this correct?
I'd like to copy the first sheet in each file only into the master report. As long as the ranges target the entered data and no further than deleting the blank rows won't be necessary. I hope that makes sense.
 
Upvote 0
I'm getting an error. Debug is highlighting the following as an issue:
Set Wbk = Workbooks.Open(Pth & Fname)

The first worksheet in my master report file is named "Master". The directory is definitely "C:\AllFiles".
 
Upvote 0
Change this line as shown
Code:
   Fname = Dir([COLOR=#ff0000]Pth &[/COLOR] "*.xls*")
 
Upvote 0
If you add this message box
Code:
   Do While Fname <> ""
      MsgBox Pth & vbLf & Fname
      Set Wbk = Workbooks.Open(Pth & Fname)
What does it say & does it open the file?
Also what error are you getting?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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