VBA Consolidation Code

danachu

New Member
Joined
Jul 31, 2019
Messages
4
Hi all, I need help with a VBA Code. I have an excel table that looks like this:

POStatusDescriptionVendorDepartmentDate ApprovedProject IDBefore TaxTaxAfter TaxInvoice NumberInvoice DateInvoice Amount
100012ClosedReplenish Motors 1935.70AIT7502017-06-06T07:00:552
3053520001237/23/201865
100012ClosedReplenish Motors 1935.70AIT7522017-06-06T07:00:5522042420001237/23/201865

<tbody>
</tbody>


<tbody>
</tbody>


I want to consolidate any of the rows that have the same PO numbers and make it look like the table below:
Basically, I want all the columns except for the Before Tax, Tax, and After Tax columns to be consolidated into one value. If the cell values in any of those columns don't match, I would like those values to both be represented in the corresponding combined cell, and separated by a comma (this is demonstrated in the Department column below). For the Before Tax, Tax, and After Tax columns, I would like the values to be added up and the row to just show the grand total. If someone could please help to formulate a VBA code that can do this that would be a huge help. Thank you!
POStatusDescriptionVendorDepartmentDate ApprovedProject IDBefore TaxTaxAfter TaxInvoice NumberInvoice DateInvoice Amount
100012ClosedReplenish Motors 1935.70AIT750, 7522017-06-06T07:00:5525095920001237/23/201865


<tbody>
</tbody>

 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Aug42
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant, ray [COLOR="Navy"]As[/COLOR] Variant, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
ray = Range("A1").CurrentRegion
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] n = 1 To UBound(ray, 1)
    [COLOR="Navy"]If[/COLOR] Not Dic.Exists(ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
        ReDim nray(1 To UBound(ray, 2))
        [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(ray, 2)
            nray(Ac) = ray(n, Ac)
        [COLOR="Navy"]Next[/COLOR] Ac
        Dic.Add ray(n, 1), nray

    [COLOR="Navy"]Else[/COLOR]
        Q = Dic(ray(n, 1))
         [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(ray, 2)
            [COLOR="Navy"]If[/COLOR] Ac = 8 Or Ac = 9 Or Ac = 10 [COLOR="Navy"]Then[/COLOR]
                Q(Ac) = Q(Ac) + ray(n, Ac)
            [COLOR="Navy"]ElseIf[/COLOR] Not Q(Ac) = ray(n, Ac) [COLOR="Navy"]Then[/COLOR]
                 Q(Ac) = Q(Ac) & ", " & ray(n, Ac)
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] Ac
        Dic(ray(n, 1)) = Q
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] Dic.keys
c = c + 1
    [COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Cells(c, 1).Resize(, UBound(Dic(K)))
        .Value = Dic(K)
        .ColumnWidth = 10
        .WrapText = True
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,

I was wondering if you could modify the above code so that the invoice amount column is also added up to a grand total like the before tax, tax, and after tax columns.
 
Upvote 0
No problem, Add code shown in Red !!
Code:
For Ac = 1 To UBound(ray, 2)
            If Ac = 8 Or Ac = 9 Or Ac = 10 [COLOR="#FF0000"][SIZE=3][B]Or Ac = 13 [/B][/SIZE][/COLOR]Then
                Q(Ac) = Q(Ac) + ray(n, Ac)
            ElseIf Not Q(Ac) = ray(n, Ac) Then
                 Q(Ac) = Q(Ac) & ", " & ray(n, Ac)
            End If
        Next Ac
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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