Code for anwser to this Thread:
http://www.excelfox.com/forum/showth...e-folder/page2
Code:
Option Explicit
Sub consolidateToo() ' http://www.excelfox.com/forum/showthread.php/2238-Copy-data-from-Unique-files-into-Masterfile-all-the-files-in-the-same-folder?p=10595#post10595
Rem 1 ThisWorkbook Info
Dim MWs1 As Worksheet: Set MWs1 = ThisWorkbook.Worksheets.Item(1) 'Worksheets("OriginalData")
Dim DtaFName As String: Let DtaFName = VBA.Dir(ThisWorkbook.Path & "\" & "*.xlsx") ' Search criteria set to all Files with .xlsx extension in the same Folder as this workbook, Dir returns first file name that fits criteria
Dim LrMWs1 As Long: Let LrMWs1 = MWs1.Range("A" & MWs1.Rows.Count & "").End(xlUp).Row
Rem 2 main Loop for all data files
Do While DtaFName <> "" ' ==========================================
Workbooks.Open filename:=ThisWorkbook.Path & "\" & DtaFName
Dim WBDta As Workbook: Set WBDta = ActiveWorkbook
Dim WBDtaWs1 As Worksheet: Set WBDtaWs1 = WBDta.Worksheets.Item(1) ' use variable to reference the first worksheet ( counting tabs from the left ) of last opened and therefore active( to be seen ) file
Dim arrIn() As Variant: Let arrIn() = WBDtaWs1.Range("A1").CurrentRegion.Value
'2a) loop for all data rows, copy data from completed rows to master file, ( add date to inputed data array '_-##)
Dim Rw As Long ' --------------------------------
For Rw = 2 To UBound(arrIn(), 1) ' loop through "rows" in data array
If arrIn(Rw, 11) <> Empty And arrIn(Rw, 12) = Empty Then ' Condition for completed work not yet consolidated
Dim arrCsDte(1 To 1, 1 To 7) As String: Let arrCsDte(1, 1) = arrIn(Rw, 7): arrCsDte(1, 2) = arrIn(Rw, 8): arrCsDte(1, 3) = arrIn(Rw, 9): arrCsDte(1, 4) = arrIn(Rw, 10): arrCsDte(1, 5) = arrIn(Rw, 11): arrCsDte(1, 6) = Format(Date, "dd.mmm.yyyy"): arrCsDte(1, 7) = arrIn(Rw, 13) ' 7 "columns" of data to be added to master file
MWs1.Range("A2:A" & LrMWs1 & "").Find(what:=arrIn(Rw, 1), After:=MWs1.Range("A2"), LookIn:=xlValues, Lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext).Offset(0, 6).Resize(1, 7) = arrCsDte() ' We look down the first column in the master file to find the cell comtaining the S No We apply the offest property to thast cell to get across to column G and then the resize property gives us the range of 7 columns to which we may apply the values in the array filled for the row data
Let arrIn(Rw, 12) = arrCsDte(1, 6) '(Put the current date in the array made from data range '_-##)
Else ' Datá row is completed and consolidated , so nothing to do for this row
End If
Next Rw ' End loop for all data rows --------
'2b) Update and close current data workbook
Let WBDtaWs1.Range("A1").Resize(UBound(arrIn(), 1), UBound(arrIn(), 2)).Value = arrIn() ' reassign the values from the input data array back to the range as this now has the consolidated date in it
WBDta.Close savechanges:=True
'2c Serch for next data file name
Let DtaFName = VBA.Dir() ' Unqualified Dir returns next found file with previos search criteria, but only returns each file name once
Loop ' Do While DtaFName <> "" again ==============================
End Sub
Bookmarks