Vba to filter table and add total row when cell value changes in column

KGEORGE13

New Member
Joined
May 30, 2018
Messages
36
HELLO,

I HAVE A WORKSHEET THAT LOOKS SOMETHING LIKE THIS...

QUANTITY
PART #
4
A
2
C
1
B
3
A
1
A
2
B
5
C

<tbody>
</tbody>

I AM LOOKING FOR A VBA TO SORT AND FILTER THE TABLE TO LOOK LIKE ...


QUANTITY
PART #
4
A
3
A
1
A
8
TOTAL
1
B
2
B
3
TOTAL
2
C
5
C
7
TOTAL

<tbody>
</tbody>


THIS WILL BE A CONTINUOUSLY RUNNING TEMPLATE TO COPY ORDER FORMS IN, SO IT NEEDS TO BE ABLE TO HANDLE THAT.


THANK YOU!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Maybe this to overwrite columns "A & B".
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Sep44
[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] K [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nSum [COLOR="Navy"]As[/COLOR] Double
[COLOR="Navy"]Set[/COLOR] Rng = Range("B2", Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR] .Add Dn.Value, New Collection
        .Item(CStr(Dn.Value)).Add CStr(Dn.Offset(, -1).Value)
    [COLOR="Navy"]Next[/COLOR]
ReDim Ray(1 To 2, 1 To 1)
c = 1
Ray(1, 1) = "QUANTITY": Ray(2, 1) = "PART#"
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
     [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] .Item(K)
          c = c + 1
          ReDim Preserve Ray(1 To 2, 1 To c)
          Ray(1, c) = P
          Ray(2, c) = K
          nSum = nSum + P
     [COLOR="Navy"]Next[/COLOR] P
        c = c + 1
        ReDim Preserve Ray(1 To 2, 1 To c)
        Ray(1, c) = nSum
        Ray(2, c) = "TOTAL"
        nSum = 0
 [COLOR="Navy"]Next[/COLOR] K
Range("a1").Resize(c, 2) = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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