VBA for custom subtotal based on conditions

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am making a Macro to clean up some data but one task that has always eluded me was an efficient method to subtotal quantities based on conditions (See example below).

I have used subtotal previously but it only allows one condition (as far as I am aware) and can be slow depending on the volume of data.

What I want to do is take the "BEFORE" data and have a macro clean up the data including Subtotaling the Qty for every Same Part number with the Same condition.
so Every Throttle Shaft who's part number is 2543391 and has a condition of NE, subtotal Qty onto one line.

Is there a way to do this that wont bog down excel too much? (I could have 10000 rows of data).

Thank you to anyone who can help

1592588317002.png
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
How about
VBA Code:
Sub willow()
   Dim Ary As Variant
   Dim Dic As Object
   Dim i As Long
   Dim Tmp As String
   
   Set Dic = CreateObject("scripting.dictionary")
   Ary = Range("A1").CurrentRegion.Value2
   
   For i = 2 To UBound(Ary)
      Tmp = Ary(i, 1) & "|" & Ary(i, 2) & "|" & Ary(i, 3)
      Dic(Tmp) = Dic(Tmp) + Ary(i, 4)
   Next i
   
   With Range("G2").Resize(Dic.Count)
      .Value = Application.Transpose(Dic.Keys)
      .TextToColumns Range("G2"), xlDelimited, xlTextQualifierDoubleQuote, , 0, 0, 0, 0, 1, "|"
      .Offset(, 3).Value = Application.Transpose(Dic.Items)
   End With
 
Upvote 0
Forgive if this is a newbie response, but what about using the SumIfs function?
 
Upvote 0
This works perfect. Instead of having the results in column G is it possible to have it go into another sheet at A1? (We will call it Sheet2 for this example)
 
Upvote 0
I don't see how to use the SumIFs function accurately since I want to remove the duplicated rows. Fluff Macro works however I just need to put the data on another sheet instead of the same sheet. I also need to keep the headers...
 
Upvote 0
I just finished a subroutine that copied all of the projected invoice dates for product and installation service charges in a column (with duplicates) onto a "data" sheet where I remove the duplicates, blank lines (because I grabbed all of the values in a column). I then format, sort and use the sumif to roll up the amounts to be invoiced by date. The result is being placed onto a Summary sheet with rolled up data that will serve as an "executive summary" for those who don't like all of the detail on the main sheet. Still working on the latter at the moment, but it is coming together.
 
Upvote 0
How about
VBA Code:
Sub willow()
   Dim Ary As Variant
   Dim Dic As Object
   Dim i As Long
   Dim Tmp As String
   
   Set Dic = CreateObject("scripting.dictionary")
   Ary = Range("A1").CurrentRegion.Value2
   
   For i = 1 To UBound(Ary)
      Tmp = Ary(i, 1) & "|" & Ary(i, 2) & "|" & Ary(i, 3)
      Dic(Tmp) = Dic(Tmp) + Ary(i, 4)
   Next i
   
   With Sheets("Sheet2").Range("A1").Resize(Dic.Count)
      .Value = Application.Transpose(Dic.Keys)
      .TextToColumns Range("A1"), xlDelimited, xlTextQualifierDoubleQuote, , 0, 0, 0, 0, 1, "|"
      .Offset(, 3).Value = Application.Transpose(Dic.Items)
   End With
End Sub
 
Upvote 0
I just finished a subroutine that copied all of the projected invoice dates for product and installation service charges in a column (with duplicates) onto a "data" sheet where I remove the duplicates, blank lines (because I grabbed all of the values in a column). I then format, sort and use the sumif to roll up the amounts to be invoiced by date. The result is being placed onto a Summary sheet with rolled up data that will serve as an "executive summary" for those who don't like all of the detail on the main sheet. Still working on the latter at the moment, but it is coming together.

I think I understand what you are saying. That is a good method as well. I will keep that in mind.

Thank you very much :)
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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