+ Reply to Thread
Results 1 to 4 of 4

Returning A Sum From Multiple Occurences

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2013
    Location
    Texas
    MS-Off Ver
    Excel 2016
    Posts
    8

    Returning A Sum From Multiple Occurences

    I am trying to sum the total number of a certain part used on different jobs. The worksheet is set up as follows:

    Assembly #1 # for Job #1 # for Job #2 # for Job #3
    Part #2 2 3 5
    Part #4 1 1 1
    Part #8 3 2 1
    Part #10 6 4 2
    Part #15 5 1 3

    Assembly #2
    Part #2 2 2 2
    Part #4 1 2 3
    Part #6 5 1 8
    Part #7 6 8 7

    Then I would filter the parts to determine the unique parts. In this case I would create a list like the one below:

    Part #2
    Part #4
    Part #6
    Part #7
    Part #8
    Part #10
    Part #15

    At this point I need something that will take each unique part from this list and then come up with the total number of each part (adding all three jobs together).

    I was imaging something with an if statement that if analyzed the range of assemblies (which could be 100 different assemblies) and determined the total number of parts across all the jobs (but only for unique entries) which would require it to sum multiple lines. I just can't for the life of me come up with code that would do this.

    Thanks for any help.
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Returning A Sum From Multiple Occurences

    Hello cdwelch4,

    The attached workbook has the macro below. A button on the worksheet runs the macro. The output list of part numbers and their required totals appears in column "J:K" on the same sheet.
    Sub Macro1()
    
        Dim AssyQty As Variant
        Dim Cell    As Range
        Dim Count   As Long
        Dim Dict    As Object
        Dim Jobs    As Range
        Dim Key     As Variant
        Dim SrcRng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            Set SrcRng = Wks.Range("A1").CurrentRegion
            Set DstRng = Wks.Range("J1:K1")
            
                DstRng.Value = Array("Part #", "Total Req'd")
            
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                Set SrcRng = Intersect(SrcRng, SrcRng.Offset(2, 1))
                
                For r = 1 To SrcRng.Rows.Count
                    Key = SrcRng.Item(r, 1)
                    AssyQty = SrcRng.Item(r, 2)
                    Set Jobs = Intersect(SrcRng.Rows(r), SrcRng.Rows(r).Offset(0, 2))
                        If Not IsNumeric(AssyQty) Then
                            Count = Application.Sum(Jobs)
                        Else
                            Count = AssyQty * Application.Sum(Jobs)
                        End If
                    If Not IsEmpty(Key) Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, Count
                        Else
                            Count = Dict(Key) + Count
                            Dict(Key) = Count
                        End If
                    End If
                Next r
                
            r = 0
            For Each Key In Dict.Keys
                r = r + 1
                DstRng.Offset(r, 0).Value = Array(Key, Dict(Key))
            Next Key
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-22-2013
    Location
    Texas
    MS-Off Ver
    Excel 2016
    Posts
    8

    Re: Returning A Sum From Multiple Occurences

    That is great. Thank you so much. Is there a way to show two columns before the "Total Req'd" column that show the total for each job? To where it shows the total for Job #1, total for Job #2 and then the "Total Req'd" for all jobs.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Returning A Sum From Multiple Occurences

    Hello cdwelch4,

    This macro is a augmented version of the previous one. This list the Part Number, Jobs, Job counts, and the Total for each job starting in column "J". The output list is now sorted from smallest to largest part number.

    Here is the improved code...
    Option Explicit
    
    Sub Macro1()
    
        Dim AssyQty As Variant
        Dim Cell    As Range
        Dim Count   As Variant
        Dim Counts  As Variant
        Dim Dict    As Object
        Dim DstRng  As Range
        Dim Header  As Variant
        Dim Jobs    As Range
        Dim Key     As Variant
        Dim n       As Long
        Dim r       As Long
        Dim SrcRng  As Range
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            Set SrcRng = Wks.Range("A1").CurrentRegion
            Set DstRng = Wks.Range("J1")
            
                ReDim Header(SrcRng.Columns.Count - 3 + 1)
                    Header(0) = "Part #"
                    Header(UBound(Header)) = "Total"
                
                    For n = 1 To UBound(Header) - 1
                        Header(n) = SrcRng.Cells(2, n + 3)
                    Next n
                    
              ' Format the header row of the destination.
                Set DstRng = DstRng.Resize(1, UBound(Header) + 1)
                DstRng.Value = Header
                DstRng.Font.Bold = True
                DstRng.HorizontalAlignment = xlHAlignRight
            
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                Set SrcRng = Intersect(SrcRng, SrcRng.Offset(2, 1))
                
                For r = 1 To SrcRng.Rows.Count
                    Key = SrcRng.Item(r, 1)
                    
                    AssyQty = SrcRng.Item(r, 2)
                        If Not IsNumeric(AssyQty) Then AssyQty = 1 Else AssyQty = CDbl(AssyQty)
                        
                    Set Jobs = Intersect(SrcRng.Rows(r), SrcRng.Rows(r).Offset(0, 2))
                        ReDim Counts(1 To Jobs.Columns.Count + 1)
                        n = 0
                        For Each Count In Jobs
                            n = n + 1
                            Counts(n) = AssyQty * Count.Value
                        Next Count
                        
                    If Not IsEmpty(Key) Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, Counts
                        Else
                            Counts = Dict(Key)
                            n = 0
                            For Each Count In Jobs
                                n = n + 1
                                Counts(n) = Counts(n) + (AssyQty * Count.Value)
                            Next Count
                            Dict(Key) = Counts
                        End If
                    End If
                
               Next r
                
          ' Output the Part Numbers and Job Numbers with their individual and total counts.
            Application.ScreenUpdating = False
                r = 0
                For Each Key In Dict.Keys
                    r = r + 1
                    DstRng.Offset(r, 0).Value = Key
                    Counts = Dict(Key)
                    Counts(UBound(Counts)) = Application.Sum(Counts)
                    DstRng.Offset(r, 1).Resize(1, Jobs.Columns.Count + 1).Value = Counts
                Next Key
            
              ' Sort the Parts Numbers from smallest to largest.
                Set DstRng = DstRng.Resize(RowSize:=r)
            
                Wks.Sort.SortFields.Clear
                Wks.Sort.SortFields.Add Key:=DstRng.Item(2, 1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            
                With Wks.Sort
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SetRange DstRng
                    .Apply
                End With
            Application.ScreenUpdating = True
            
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find Multiple Occurences from Multiple Sources
    By erome in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-24-2012, 01:16 PM
  2. Finding Occurences of "True" and returning the Correlating Description
    By braydon16 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-23-2011, 03:56 PM
  3. returning count of unique occurences of a string from a range.
    By tinkerbelle in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-10-2011, 04:50 PM
  4. Replies: 3
    Last Post: 08-11-2010, 11:42 AM
  5. Replies: 2
    Last Post: 06-23-2009, 05:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1