I am trying to combine ~300 workbooks into one single workbook. All 300 workbooks have the exact same header. I tried using the code from thread https://www.excelforum.com/showthread.php?p=696435 but nothing is being copied over. The only difference between my example and the other is I only need to take data from the first sheet in each data workbook. All the workbooks are located in following directory
F:\Excel Tips\Combine Workbooks\WorkbookData
The “master file” is located in another directory. The “master file” also has the same header as the data workbooks. Basically, I want to retrieve all data (excluding the header) from the first data workbook and copy to the master file. Then I want to go to the second workbook and retrieve all data from the second data workbook and copy to master file, and so on. The code I am using to combine is as follows:
Sub Get_Value_From_All()
Dim wbSource As Workbook
Dim wbThis As Workbook
Dim rToCopy As Range
Dim uRng As Range
Dim rNextCl As Range
Dim lCount As Long
Dim bHeaders As Boolean
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
On Error Resume Next
Set wbThis = ThisWorkbook
'clear the range except headers
Set uRng = wbThis.Worksheets(1).UsedRange
If uRng.Cells.Count <= 1 Then
'no data in master sheet
bHeaders = False
GoTo search
End If
uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
uRng.Columns.Count).Clear
search:
With .FileSearch
.NewSearch
'Change path to suit
.LookIn = "F:\Excel Tips\Combine Workbooks\WorkbookData"
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.Count ' Loop through all.
'Open Workbook x and Set a Workbook variable to it
Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
For i = 1 To Sheets.Count - 1
Set rToCopy = wbSource.Worksheets(i).UsedRange
Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
If bHeaders Then
'headers exist so don't copy
rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
rToCopy.Columns.Count).Copy rNextCl
'no headers so copy
'place headers in Row 2
Else: rToCopy.Copy Cells(1, 1)
bHeaders = True
End If
Next i
wbSource.Close False 'close source workbook
Next lCount
Else: MsgBox "No workbooks found"
End If
End With
CalcMode = .Calculation
.Calculation = xlCalculationManual
End With
'not checked following code
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
On Error GoTo 0
ScreenUpdating = True
DisplayAlerts = True
EnableEvents = True
'End With
End Sub
Thank you in advance for any assistance.
Bookmarks