Modify this Code to add-up numbers in COL-D

Guanjin Peter

Active Member
Joined
May 21, 2008
Messages
429
It's a linked post from:
http://www.excelforum.com/showthread.php?t=647543

I managed to do the combine the row if column B matches. However column D(quantity, number value), i want the quantity to add-up if column B matches. Any idea how do I modify the code below to do that?

for example:
TDG-**002 Tuna Cheese Pizza Bar (KG) KG 30
TDG-**002 Tuna Cheese Pizza Bar (MG) MG 30
TDG-**002 Tuna Cheese Pizza Bar (KG) KG 30

will combine to become
TDG-**002 Tuna Cheese Pizza Bar (KG) KG 60 <<<< combined
TDG-**002 Tuna Cheese Pizza Bar (MG) MG 30 <<<< left untouched

Code:
 Dim lngTMP As Long, iRows As Long
    On Error GoTo Fin
    Application.ScreenUpdating = False
    iRows = Cells(Cells.Rows.Count, 2).End(xlUp).Row
    For lngTMP = iRows To 6 Step -1
        If WorksheetFunction.CountIf(Columns(2), Cells(lngTMP, 2)) > 1 Then
            Rows(lngTMP).Delete
        End If
    Next lngTMP
Fin:
    Application.ScreenUpdating = True

Code:
[LEFT]Private Sub CommandButton1_Click()

  Dim wb As Workbook, ws As Worksheet, i As Integer, filess As String
  ChDrive Left(ThisWorkbook.Path, 1)
  ChDir ThisWorkbook.Path
  filess = Dir("template(*).xls")
  While filess <> ""
    Set wb = Workbooks.Open(filess)
    Set ws = wb.Sheets("Sheet1")
    Dim intRow As Integer
    intRow = 1
            Do While ws.Cells(4 + intRow, 1).Value <> ""
            i = i + 1
                proid = ws.Cells(4 + intRow, 1).Value
                pro = ws.Cells(4 + intRow, 2).Value
                uom = ws.Cells(4 + intRow, 3).Value
                qty = ws.Cells(4 + intRow, 4).Value
                ThisWorkbook.Sheets("GrandTotal").Range("a" & 1 + i).End(xlUp).Offset(1, 0).Resize(, 4) = Array(proid, pro & " (" & uom & ")", uom, qty)
                intRow = intRow + 1
            Loop
    ws.Range("a1:d4").Copy
    wb.Close savechanges:=False
    filess = Dir()
  Wend
  
    Dim lngTMP As Long, iRows As Long
    On Error GoTo Fin
    Application.ScreenUpdating = False
    iRows = Cells(Cells.Rows.Count, 2).End(xlUp).Row
    For lngTMP = iRows To 6 Step -1
        If WorksheetFunction.CountIf(Columns(2), Cells(lngTMP, 2)) > 1 Then
            Rows(lngTMP).Delete
        End If
    Next lngTMP
Fin:
    Application.ScreenUpdating = True
    
    Rows("1:1").Insert Shift:=xlDown
    Rows("1:1").Insert Shift:=xlDown
    Rows("1:1").Insert Shift:=xlDown
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

End Sub[/LEFT]
 
Found Jindon's code just need to modify this?

Code:
Dim a, b(), i As Long, ii As Integer, n As Long, z As String
With Sheets("grandTotal").Range("a1").CurrentRegion
    a = .Value
    If .Column<> 1 Then
        a = .Offset(, -1).Resize(, .Columns.Count).Value
    End If
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    For i = 3 To UBound(a, 2)
        b(1, i) = a(1, i): b(2, i) = a(2, i)
    Next
    n = 2
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextCompare
        For i = 3 To UBound(a, 1)
            z = a(i, 1) & ";" & a(i, 2)
            If Not .exists(z) Then
                n = n + 1: b(n, 1) = a(i, 1): b(n, 2) = a(i, 2): .Add z, n
            End If
            For ii = 3 To UBound(a, 2)
                 b(.Item(z), ii) = b(.Item(z), ii) + a(i, ii)
            Next
        Next
    End With
    With Sheets("grandTotal").Range("a1").CurrentRegion.Cells(1)
    'With .Offset(.Rows.Count + 1, IIf(.Column<> 1, -1, 0)).Resize(1, 1)
        .CurrentRegion.ClearContents
        .Resize(n, UBound(b, 2)).Value = b
    End With
End With

before
protoV6.xls
ABCD
1
2TDG-**002TunaCheesePizzaBar(KG)KG30
3RFG-**588ThaiChickenFriedRice(KG)KG30
4RFG-**502ClubSandwich(PKT)PKT6
5RFG-**501RoastMexicanChicken(PKT)PKT30
6RFG-**501RoastMexicanChicken(pcs)pcs24
7TDG-**002TunaCheesePizzaBar(pcs)pcs30
8RFG-**501RoastMexicanChicken(KG)KG20
9TDG-**002TunaCheesePizzaBar(KG)KG30
10RFG-**588ThaiChickenFriedRice(KG)KG30
11RFG-**502ClubSandwich(PKT)PKT6
12RFG-**501RoastMexicanChicken(PKT)PKT30
13RFG-**501RoastMexicanChicken(pcs)pcs24
14TDG-**002TunaCheesePizzaBar(pcs)pcs30
15RFG-**501RoastMexicanChicken(KG)KG20
16TDG-**002TunaCheesePizzaBar(KG)KG30
17RFG-**588ThaiChickenFriedRice(KG)KG30
18RFG-**502ClubSandwich(PKT)PKT6
19RFG-**501RoastMexicanChicken(PKT)PKT30
20RFG-**501RoastMexicanChicken(pcs)pcs24
21TDG-**002TunaCheesePizzaBar(pcs)pcs30
22RFG-**501RoastMexicanChicken(KG)KG20
GrandTotal


after
protoV6.xls
ABCD
1
2KG30
3RFG-**588ThaiChickenFriedRice(KG)KGKGKG90
4RFG-**502ClubSandwich(PKT)PKTPKTPKT18
5RFG-**501RoastMexicanChicken(PKT)PKTPKTPKT90
6RFG-**501RoastMexicanChicken(pcs)pcspcspcs72
7TDG-**002TunaCheesePizzaBar(pcs)pcspcspcs90
8RFG-**501RoastMexicanChicken(KG)KGKGKG60
9TDG-**002TunaCheesePizzaBar(KG)KGKG60
GrandTotal
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Guys I'm very close already, just need a litte bit more help. As you can see, column C output (seperate table below) looks really wrong. I know it's something got to do with the code highlight in red.

Code:
Dim a, b(), c(), i As Long, ii As Integer, n As Long, z As String
With Sheets("grandTotal").Range("a1").CurrentRegion
    a = .Value
    If .Column<> 1 Then
        a = .Offset(, -1).Resize(, .Columns.Count).Value
    End If
    
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    For i = 2 To UBound(a, 2) 'affects column
        b(1, i) = a(1, i): b(2, i) = a(2, i)
        
[COLOR="Red"][B]    ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
        c(1, i) = a(1, i): c(2, i) = a(2, i)[/B][/COLOR]

    Next
    n = 1 ' affect the rows, correct if 1
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextCompare
        For i = 2 To UBound(a, 1)            ' affects how data is shown, correct if i=2
            z = a(i, 1) & ";" & a(i, 2)
            If Not .exists(z) Then
                n = n + 1: b(n, 1) = a(i, 1): b(n, 2) = a(i, 2): .Add z, n
            End If
            For ii = 3 To UBound(a, 2)  ' affects combined data, correct if ii=3
                 b(.Item(z), ii) = b(.Item(z), ii) + a(i, ii)
            Next
        Next
    End With
    'With Sheets("grandTotal").Range("a1").CurrentRegion.Cells(1)
    With .Offset(.Rows.Count + 1, IIf(.Column<> 1, -1, 0)).Resize(1, 1)
        .CurrentRegion.ClearContents
        .Resize(n, UBound(b, 2)).Value = b
    End With
End With
protoV6.xls
ABCD
1
2TDG-**002TunaCheesePizzaBar(KG)KG30
3RFG-**588ThaiChickenFriedRice(KG)KG30
4RFG-**502ClubSandwich(PKT)PKT6
5RFG-**501RoastMexicanChicken(PKT)PKT30
6RFG-**501RoastMexicanChicken(pcs)pcs24
7TDG-**002TunaCheesePizzaBar(pcs)pcs30
8RFG-**501RoastMexicanChicken(KG)KG20
9TDG-**002TunaCheesePizzaBar(KG)KG30
10RFG-**588ThaiChickenFriedRice(KG)KG30
11RFG-**502ClubSandwich(PKT)PKT6
12RFG-**501RoastMexicanChicken(PKT)PKT30
13RFG-**501RoastMexicanChicken(pcs)pcs24
14TDG-**002TunaCheesePizzaBar(pcs)pcs30
15RFG-**501RoastMexicanChicken(KG)KG20
16TDG-**002TunaCheesePizzaBar(KG)KG30
17RFG-**588ThaiChickenFriedRice(KG)KG30
18RFG-**502ClubSandwich(PKT)PKT6
19RFG-**501RoastMexicanChicken(PKT)PKT30
20RFG-**501RoastMexicanChicken(pcs)pcs24
21TDG-**002TunaCheesePizzaBar(pcs)pcs30
22RFG-**501RoastMexicanChicken(KG)KG20
23
24
25TDG-**002TunaCheesePizzaBar(KG)KGKGKGKG120
26RFG-**588ThaiChickenFriedRice(KG)KGKGKG90
27RFG-**502ClubSandwich(PKT)PKTPKTPKT18
28RFG-**501RoastMexicanChicken(PKT)PKTPKTPKT90
29RFG-**501RoastMexicanChicken(pcs)pcspcspcs72
30TDG-**002TunaCheesePizzaBar(pcs)pcspcspcs90
31RFG-**501RoastMexicanChicken(KG)KGKGKG60
GrandTotal
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top