Selected Answer
Hasson
In the attached revised file (name changed), I've modified your macro as shown in bold below (with comments you can remove). It needs to be run in a folder containing your other files (however many).
I've added some variables to reduce the code but essentially it now:
- clears the results in sheet RP
- adds the quatiities from column E of the other files (into column C)
- removes your line using the (Range) .RemoveDuplicates method and
- sorts the results, applies subtotals (then collapses them to show only the totals) before
- using a simple loop to number the (visible) subtotal items.
It says things like "Item 1" since you can use the Subtotal controls to show the (level 3) detail i.e. which item a quantity was in another file.
Sub PullData()
Dim MyObj As Object, MySource As Object, file As Variant
Dim wb As Workbook, wbMe As Workbook
Dim ws As Worksheet, wsMe As Worksheet
Dim Path As String
Dim wsLstRw As Long, wsLstSub As Long, n as Long
Path = ThisWorkbook.Path & "\"
file = Dir(Path)
Set wbMe = ThisWorkbook: Set wsMe = wbMe.Sheets("RP")
'clear existing results
wsMe.Range("A2").CurrentRegion.Offset(1, 0).EntireRow.Delete
While (file <> "" And LCase(file) <> "result.xlsm") ' ### changed file name
Set wb = Workbooks.Open(Path & file): Set ws = wb.Sheets(1)
'get last item in A
wsLstRw = ws.Range("A" & Rows.Count).End(xlUp).Row
ws.Range("A2:B" & wsLstRw).Copy wsMe.Range("A" & wsMe.Range("A" & Rows.Count).End(xlUp).Row + 1)
'also copy quantities to column C
ws.Range("E2:E" & wsLstRw).Copy wsMe.Range("C" & wsMe.Range("C" & Rows.Count).End(xlUp).Row + 1)
wb.Close False
file = Dir
Wend
'sort values in B
wsMe.Range("A:C").Sort Key1:=wsMe.Range("B2"), Order1:=xlAscending, Header:=xlYes
' add sub-totals to C when B changes
wsMe.Range("A2").CurrentRegion.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=3
'collapse to show subtotals only
wsMe.Outline.ShowLevels RowLevels:=2
'get row
wsLstSub = wsMe.Range("C" & Rows.Count).End(xlUp).Row
' loop through visible cells and add text + number
For Each Cl In Range("A2:A" & wsLstSub - 1).SpecialCells(xlCellTypeVisible)
n = n + 1
Cl.Value = "Item " & n
Next Cl
End Sub
If you don't want to use subtotals, the second file has slightly different code and uses a loop (running backwards from the last data row to row and deleting matching rows after summing data). Heres' just that code segment (after the Wend line) with comments:
'sort values in B
wsMe.Range("A:C").Sort Key1:=wsMe.Range("B2"), Order1:=xlAscending, Header:=xlYes
' loop backwards, adding numbers if B matches
For n = wsMe.Range("C" & Rows.Count).End(xlUp).Row To 3 Step -1
'Look at value in B and if matching row above, add quantity to row above and delete row
With wsMe.Cells(n, 2)
If .Value = .Offset(-1, 0).Value Then
.Offset(-1, 1).Value = .Offset(-1, 1).Value + .Offset(0, 1).Value
.EntireRow.Delete
End If
End With
Next n
' renumber items
wsMe.Range("A2") = 1: wsMe.Range("A2").AutoFill Destination:=wsMe.Range("A2:A" & wsMe.Range("A" & Rows.Count).End(xlUp).Row), Type:=xlFillSeries
End Sub
Note that (as with the first file), if you paste the code into another file, the file name in the line
While (file <> "" And LCase(file) <> "result via loop.xlsm") ' ### changed file name
must be changed to match your file name.
Hope this makes sense and helps.