Sub collectdata()
Dim FileToOpen$, k&, i&, j&, jn$, cl&, jnc$, c&, h&, nr&
Dim DataWb As Workbook, OutWB As Workbook
Dim ws As Worksheet, ou As Worksheet
Dim a, b()
Dim dict As Object
Dim rcl As Range
Application.ScreenUpdating = False
Set OutWB = ThisWorkbook
FileToOpen = ThisWorkbook.Path & "\" & "DATA-calculate data from multiple.xlsx"
Set DataWb = Workbooks.Open(Filename:=FileToOpen, ReadOnly:=True)
Set dict = CreateObject("Scripting.Dictionary")
Set ou = OutWB.Sheets("ORGINAL")
With CreateObject("Scripting.Dictionary")
k = 1
b = ou.UsedRange.Value
For j = 2 To UBound(b)
If Not .exists(b(j, 2) & b(j, 3) & b(j, 4)) Then
jn = WorksheetFunction.Concat(b(j, 2), b(j, 3), b(j, 4))
.Add jn, k
b(j, 10) = jn
k = k + 1
End If
Next j
c = ou.Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each ws In DataWb.Worksheets
a = ws.UsedRange
Set rcl = ou.[E1:I2].Find(Trim(ws.Name), , xlValues, xlPart)
cl = rcl.Column
For i = 2 To ws.Cells(Rows.Count, 1).End(xlUp).Row 'UBound(a)
jnc = WorksheetFunction.Concat(a(i, 2), a(i, 3), a(i, 4))
If .exists(jnc) Then
For h = LBound(b) To UBound(b)
If jnc = b(h, 10) Then
nr = h
End If
Next h
ou.Cells(nr, cl).Value = ou.Cells(nr, cl).Value + a(i, 5)
ou.Cells(nr, 10).FormulaR1C1 = "=RC[-5]+RC[-4]-RC[-3]-RC[-2]+RC[-1]"
Else
ou.Cells(c, 2).Value = a(i, 2)
ou.Cells(c, 3).Value = a(i, 3)
ou.Cells(c, 4).Value = a(i, 4)
ou.Cells(c, cl).Value = ou.Cells(c, cl).Value + a(i, 5)
ou.Cells(c, 1).Value = ou.Cells(c - 1, 1).Value + 1
b = Application.Transpose(b)
ReDim Preserve b(1 To 10, 1 To c)
b = Application.Transpose(b)
b(c, 10) = jnc
c = c + 1
.Add jnc, k
k = k + 1
End If
h = 0
Next i
Next ws
End With
DataWb.Close False
ou.Range("J2:J" & c - 1).FormulaR1C1 = "=RC[-5]+RC[-4]-RC[-3]-RC[-2]+RC[-1]"
With ou.Range(Cells(2, 1), Cells(c - 1, 10))
.HorizontalAlignment = xlCenter
.Borders(7).LineStyle = xlContinuous
.Borders(7).Weight = xlThin
.Borders(8).LineStyle = xlContinuous
.Borders(8).Weight = xlThin '
.Borders(9).LineStyle = xlContinuous
.Borders(9).Weight = xlThin
.Borders(10).LineStyle = xlContinuous
.Borders(10).Weight = xlThin
.Borders(11).LineStyle = xlContinuous
.Borders(11).Weight = xlThin
.Borders(12).LineStyle = xlContinuous
.Borders(12).Weight = xlThin
End With
Application.ScreenUpdating = True
End Sub
Bookmarks