Excel VBA - How To Sort and Total Data in a Summary Tab

santa12345

Board Regular
Joined
Dec 2, 2020
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hello.
I am trying to do the following.
Lets say I have a order tab.
Main Part # is listed in column A. Qty is next. The Main Part # consists of 4 parts which is columns D - G.
I have the mid and left functions working to populate D-G... and the below screen shot.
order page.png

What I want to create...is a macro to do the following.
1. Create a summary tab.
2. Get the unique sub parts and total up the qty for each
3. The summary tab would be in the tube,top,bottom,seal order if possible.
4. Total up the qtys per sub part.

Here is the final output I am looking for on the summary tab.

summary page.png

Please let me know if you have any questions.
Any inputs and/or suggestions would be greatly appreciated.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi. will this work?
I put in a few blanks in category #3.
Thank you !!


Main Part #Qty RequestedCostExt Cost#2 - Top#3 - Bottom#4 - Seal
10-23C-ABC-v1.9
3​
0​
23CABCv1.9
10-23C-ABC-v1.10
4​
0​
23CABCv1.10
10-23C-ABC-v1.11
5​
0​
23CABCv1.11
10-23C-ABC-v1.12
3​
0​
23CABCv1.12
10-23C-ABC-v1.13
2​
0​
23CABCv1.13
10-23C-ABC-v1.14
21​
0​
23CABCv1.14
10-23C-ABC-v1.15
7​
0​
23CABCv1.15
10-23C-ABC-v1.9
6​
0​
23CABCv1.9
10-23C-ABC-v1.10
3​
0​
23CABCv1.10
10-23C-ABC-v1.11
5​
0​
22Cv1.11
10-23C-ABC-v1.12
4​
0​
24CABCv1.12
10-23C-ABC-v1.20
3​
0​
23CABCv1.20
10-23C-ABC-v1.21
2​
0​
23CABCv1.21
10-23C-ABC-v1.9
1​
0​
23CABCv1.9
10-23C-ABC-v1.10
3​
0​
23CDEFv1.10
10-23C-ABC-v1.11
4​
0​
23CDEFv1.11
10-23C-ZZZ-v2.0
100​
0​
23CZZZv2.0
10-23C-ABC-v1.12
5​
0​
23CDEFv1.12
10-23C-ABC-v1.20
3​
0​
23Cv1.20
10-23C-ABC-v1.21
2​
0​
23CDEFv1.21
10-23C-ABC-v1.9
21​
0​
23CDEFv1.9
10-23C-ABC-v1.10
7​
0​
23CDEFv1.10
10-23C-ABC-v1.11
6​
0​
23CDEFv1.11
10-23C-ABC-v1.12
3​
0​
23Cv1.12
10-23C-ABC-v1.20
5​
0​
23CABCv1.20
10-23C-ABC-v1.21
4​
0​
23Cv1.21
10-23C-ABC-v1.9
3​
0​
23CABCv1.9
10-23D-GGG-v2.4
50​
0​
23DGGGv2.4
20-24D-GGG-v2.4
50​
20​
24DGGGv2.4
69-11Z-DDD-v1.0
1000​
69​
11ZDDDv1.0
 
Upvote 0
VBA Code:
Sub CreateSummary()

Dim sht As Worksheet
Dim Lastrow As Long
Dim sht2 As Worksheet
Dim Lastrow2 As Long
Dim rownum As Long

Sheets.Add.Name = "Summary"

Set sht = Sheets("Order")
Set sht2 = Sheets("Summary")

sht2.Range("A1") = "Sub Part"
sht2.Range("B1") = "Total Qty"
Lastrow = sht.Cells(sht.Rows.Count, "D").End(xlUp).Row
sht.Range("D2:D" & Lastrow).Copy sht2.Range("A2")
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
Lastrow = sht.Cells(sht.Rows.Count, "E").End(xlUp).Row
sht.Range("E2:E" & Lastrow).Copy sht2.Range("A" & Lastrow2 + 1)
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
Lastrow = sht.Cells(sht.Rows.Count, "F").End(xlUp).Row
sht.Range("F2:F" & Lastrow).Copy sht2.Range("A" & Lastrow2 + 1)
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
Lastrow = sht.Cells(sht.Rows.Count, "G").End(xlUp).Row
sht.Range("G2:G" & Lastrow).Copy sht2.Range("A" & Lastrow2 + 1)
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row

sht2.Range("A1:A" & Lastrow2).RemoveDuplicates Columns:=Array(1), Header:=xlYes
sht2.Range("B2").FormulaR1C1 = _
    "=SUMIFS(Order!C,Order!C[2],RC[-1])+SUMIFS(Order!C,Order!C[3],RC[-1])+SUMIFS(Order!C,Order!C[4],RC[-1])+SUMIFS(Order!C,Order!C[5],RC[-1])"
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
sht2.Range("B2").Copy sht2.Range("B2:B" & Lastrow2)

sht2.Range("A1:B" & Lastrow2).Sort Key1:=sht2.Range("A1"), Order1:=xlAscending, Header:=xlYes


rownum = 2

Do Until sht2.Cells(rownum, 2) = ""
    If sht2.Cells(rownum, 1) = "" Then
        Rows(rownum).Delete
    End If
rownum = rownum + 1
Loop


rownum = 2

Do Until sht2.Cells(rownum, 1) = ""
    If sht2.Cells(rownum, 1) Like "v*" Then
        Rows(rownum).Insert
        Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
        Rows(Lastrow2).Copy Rows(rownum)
        Rows(Lastrow2).ClearContents
        Exit Sub
    End If
rownum = rownum + 1
Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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