Sub MAIN()
'ShipVought 2018 APR 4
'loops thru a specified folder containing Excel source workbooks
'OPENS each source workbook
'Loops thru each source worksheet
'copies data from selected worksheets to output table
'stores selected data from Information sheet and propagates data in output table to all rows for source workbook
'closes source workbook
Dim oFSO As Object, oFile As Object 'file system objects
Dim ws As Worksheet 'worksheet variable for source workbooks
Dim wsOUT As Worksheet 'output table worksheet
Dim rINFO As Range 'heading range for additional information columns
Dim lRowOUT As Long 'next row in output table
Dim sFolderSpec As String 'your folder path
Dim rFound As Range 'range variable to find From:, To:, Agent:
Dim sINFO(2, 1) As Variant 'array for data from Information
Dim i As Integer 'array index
Dim iCOL As Integer 'last column in output table
sINFO(0, 0) = "From:"
sINFO(1, 0) = "To:"
sINFO(2, 0) = "Agent:"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set wsOUT = Worksheets("Master")
'put your folder path here
sFolderSpec = "\\SKIPSPC\Users\Skip\Documents\TT\Test"
With wsOUT
iCOL = .Cells(1, 1).End(xlToRight).Column
Set rINFO = .Range(.Cells(1, iCOL - UBound(sINFO)), .Cells(1, iCOL))
End With
For Each oFile In oFSO.GetFolder(sFolderSpec).Files
With Workbooks.Open(oFile.Path)
'call macro to delete empty rows in source workbook
DeleteEmptyRows .Sheets(1).Parent
'loop through each sheet in source workbook
For Each ws In .Worksheets
With ws
Select Case .Name
'only copy these sheets to output table
Case "Medicines", "Disasters", "Rescues", "Dentals"
Intersect(.Range(.Cells(2, 1), .Cells(.Cells.Rows.Count, 1)).EntireRow, .UsedRange).Copy
With wsOUT
lRowOUT = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRowOUT, 1).PasteSpecial xlPasteAll
End With
'fill array from Information
Case "Information"
For i = 0 To UBound(sINFO)
Set rFound = .Columns(1).Find(sINFO(i, 0))
If Not rFound Is Nothing Then
sINFO(i, 1) = rFound.Offset(0, 1).Value
End If
Next
End Select
End With
Next
'close the workbook without saving
Application.DisplayAlerts = False
.Close
Application.DisplayAlerts = True
End With
'put the data from Information into output table
With wsOUT
lRowOUT = .Cells(.Cells.Rows.Count, iCOL).End(xlUp).Row + 1
'put the values in the right-hand columns
For i = 0 To UBound(sINFO)
.Cells(lRowOUT, iCOL + i - UBound(sINFO)).Value = sINFO(i, 1)
Next
'copy the values down to the last row in the output table
Intersect(rINFO.EntireColumn, .Rows(lRowOUT)).Copy _
Intersect(rINFO.EntireColumn, _
.Range(.Cells(lRowOUT, 1), .Cells(.UsedRange.Rows.Count, 1)).EntireRow)
End With
Next
End Sub
Sub DeleteEmptyRows(wb As Workbook)
Dim ws As Worksheet
For Each ws In wb.Worksheets
'if the row count NOT EQUAL to a count of values in column 1 then we have empty rows
If ws.UsedRange.Rows.Count <> Application.CountA(ws.Columns(1)) Then
Select Case ws.Name
Case "Information"
Case Else
'delete empty rows for all sheets other than Information
With ws.UsedRange
.AutoFilter
.AutoFilter Field:=1, Criteria1:="="
Intersect(ws.Range(ws.Cells(2, 1), ws.Cells(ws.Cells.Rows.Count, 1)).EntireRow, .Cells).Delete xlUp
.AutoFilter
End With
End Select
End If
Next
End Sub