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"
I've called the Order tab "Order".

VBA Code:
Sub CreateSummary()

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

Sheets.Add.Name = "Summary"

Set sht = Sheets("Order")
Lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set sht2 = Sheets("Summary")

sht2.Range("A1") ="Sub Part"
sht2.Range("B1") ="Total Qty"
sht.Range("D2:D" & Lastrow).Copy sht2.Range("A2")
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
sht.Range("E2:E" & Lastrow).Copy sht2.Range("A" & Lastrow2 + 1)
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
sht.Range("F2:F" & Lastrow).Copy sht2.Range("A" & Lastrow2 + 1)
Lastrow2 = sht2.Cells(sht2.Rows.Count, "A").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:=Range("A1"), Order1:=xlAscending, Header:=xlYes


End Sub
 
Upvote 0
This is fantastic ! Thank you!!!!
I assume by using the .end(x1up).row code... one line item or 1000 line items...will not matter . it will pick up everything.
I may have to have some calcs to the summary tab and copy the formula down for all populated cells in columns a&b. if that make sense.
 
Upvote 0
Something like this...

Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]*RC[-1]"
Range("D2").Select

What would be the solution for the below...instead of using static cells... as the # of rows will be different from order to order?

Selection.AutoFill Destination:=Range("D2:D14")
Range("D2:D14").Select
 
Upvote 0
Sorry one more note:
When I add a main part...screen shot attached

1620312968619.png


Summary tab shows:

1620313016353.png


Which is correct... but the sub part ZZZ is now out of order
sub part 10 - is sub part #1
sub part 23C is sub part #2
sub part ABC, DEF, ZZZ are sub parts #3
and all the v1.** are sub parts #4

I need all the sub parts sorted per group (#1) then #2 , #3, #4....

Output (summary tab) needs to be ... if possible..like this

1620313210597.png


Thank you again !!!!
 
Upvote 0
This is fantastic ! Thank you!!!!
I assume by using the .end(x1up).row code... one line item or 1000 line items...will not matter . it will pick up everything.
I may have to have some calcs to the summary tab and copy the formula down for all populated cells in columns a&b. if that make sense.

Correct, here you can see the formula added, last row calculated and formula copied down.

VBA Code:
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)
 
Upvote 0
Something like this...

Range("D2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]*RC[-1]"
Range("D2").Select

What would be the solution for the below...instead of using static cells... as the # of rows will be different from order to order?

Selection.AutoFill Destination:=Range("D2:D14")
Range("D2:D14").Select

Since the lastrow is already determined, use:

VBA Code:
sht2.Range("D2").FormulaR1C1 = "=RC[-2]*RC[-1]"
sht2.Range("D2").Copy sht2.Range("D2:D" & Lastrow2)
 
Upvote 0
Sorry one more note:
When I add a main part...screen shot attached

View attachment 38249

Summary tab shows:

View attachment 38250

Which is correct... but the sub part ZZZ is now out of order
sub part 10 - is sub part #1
sub part 23C is sub part #2
sub part ABC, DEF, ZZZ are sub parts #3
and all the v1.** are sub parts #4

I need all the sub parts sorted per group (#1) then #2 , #3, #4....

Output (summary tab) needs to be ... if possible..like this

View attachment 38251

Thank you again !!!!

Sorry, change the sort to B1 and Descending

VBA Code:
sht2.Range("A1:B" & Lastrow2).Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlYes

That'll fix the ZZZ issue. As for getting v2's after v1's..... It doesn't look like any Sort will do that.

You'll need to find a way to change the names that you can then sort by.
 
Upvote 0
Thank you for the replies.
When I change the last line of code
from
sht2.Range("A1:B" & Lastrow2).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
to
sht2.Range("A1:B" & Lastrow2).Sort Key1:=Range("B1"), Order1:=xlDescending, Header:=xlYes
I get this.

1620358683422.png

This was much closer...except for the ZZZ issue..
1620358799950.png
 
Upvote 0
You are right, I didn't read the original post correctly.

Is the only issue ZZZ? will there ever be anything else starting with W, X, Y or Z?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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