Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

summing quantity for each duplicate item across files

0

hi

I  would  way   to  summing  the  quantity  for  each   duplicate  item across files .

the  macro  brings  the  items  with  ignore  duplicates. so  I  want  fast  way  to  summing  the  quantity  for  many  files . I  put  the  expected  result  how  should  be  in file  OUTPUT .

thanks 

Answer
Discuss

Answers

0
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:

  1. clears the results in sheet RP
  2. adds the quatiities from column E of the other files (into column C)
  3. removes your line using the (Range) .RemoveDuplicates method and
  4. sorts the results, applies subtotals (then collapses them to show only the totals) before
  5. 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. 

Discuss

Discussion

Hi John
thanks    . but  truly I no  know  what  happen  for  me  . based  on  your  code  it  seems using  Pivot  TABLE I don't  want  it  .  and  when  copy  & paste   in  my  file  after  run  the macro  then  it  closes. what  I  make  mistake !!
Hasson (rep: 30) Feb 10, '22 at 5:54 pm
Hi Hasson

When you copy the code into your file, you must change the file name in this line to match your file:
While (file <> "" And LCase(file) <> "result.xlsm") ' ### changed file name
otherwise your file will be closed by the line:
wb.Close False
(that's why I added the comment "### changed file name" in my Answer.)

My Answer does NOT produce a pivot table. It simply uses sub-totals (available in the Excel ribbon under Data/ Outline/ Subtotal).

You could produce a similar result with a loop (running from the last data row to row and deleting matching rows after summing data).
John_Ru (rep: 6152) Feb 11, '22 at 3:02 am
Hasson

Please see my revised Answer (and second file), which implements the suggestion in my last paragraph above.
John_Ru (rep: 6152) Feb 11, '22 at 3:43 am
John
thanks  for   the  second  file  . this  is  much  better and  sorry  about  forget  file  name . even if  to match  file  name  with  what  is  exised  inside  the  code   the  file  close  again  .  I  no  know  what   happens  with  this  strange  case. anyway  I  choose  the  second  file . it works  as  what  I  want  , but  remains  one  thing  in  column A item .  should  auto serial numbers   arrangely 1,2,3...   . the  code   doesn't  autoserial numbers  arrangely  . may  you  fix  it,please?
Hasson (rep: 30) Feb 11, '22 at 10:33 am
Hasson

Not sure what you mean about "strange case"- the macro will close any other file in the same folder.

Please see the second file of Answer- I've now replaced since the version I uploaded did not include your line for sequential numbers:
' 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
It should now work correctly.

Note too that I changed the properties of your "Import" button to "Don't move or size with cells" (under Format.../ Properties) so that it stays where it is, even thogh lines are deleted by the macro.
John_Ru (rep: 6152) Feb 12, '22 at 3:53 am
the  matters are   much  more  better  than  before .
thanks  so  much !
Hasson (rep: 30) Feb 12, '22 at 4:21 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login