+ Reply to Thread
Results 1 to 4 of 4

Best function to sum quantities associated with text values?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-14-2010
    Location
    Houston, TX
    MS-Off Ver
    Excel 2007
    Posts
    4

    Best function to sum quantities associated with text values?

    I'm trying to sum a list of products that were sold for a particular month.

    My input data looks like this:

    Item QTY Sold QTY Paid
    Callaway Glove 2 20
    Callaway Glove 1 10
    Callaway Glove 6 55
    Odyssey Putter 1 120
    Titleist Irons 1 600
    Titleist Irons 2 1000
    .
    .
    .

    I'd like my ouput to look like this:

    Callaway Glove 9 85
    Odyssey Putter 1 120
    Titleist Irons 3 1600

    Here's one of the problems...This spreadsheet is 2000 rows long, and half of the products are duplicates (although unique sales). Is there a function that will start at the top of the spreadsheet, read through the entire spreadsheet, and then summarize everything on a line-item basis? I don't have a master product list to compare against (though this exercise will provide the basis for that going forward), thus this situation requires something a little more intuitive.

    Thanks for your help!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Best function to sum quantities associated with text values?

    You can highlight the column of Item descriptions and use the Advanced Filter to create a list of [x] Unique Values only copied to another location.

    Once you have that new unique list, the function you want to use to collect all the totals in one column based on matching a single value to an adjacent column is SUMIF().

    =SUMIF(MatchRange, MatchValue, SumRange)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    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: Best function to sum quantities associated with text values?

    Hello golfretailer,

    The attached workbook has a button to run the macro below. This will create a summary of the stock line items on a separate sheet. Each time the macro is run, the previous summary data is deleted. The summary is sort in ascending order. Click the button and your done.
    Sub Summarize()
    
      Dim Cell As Range
      Dim DstWks As Worksheet
      Dim QtyData() As Variant
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      Dim StockItems As Object
      Dim StockItem As Variant
      
        Set SrcWks = ActiveSheet
        Set DstWks = Worksheets("Summary")
        
        Set StockItems = CreateObject("Scripting.Dictionary")
        StockItems.CompareMode = vbTextCompare
        
          Set Rng = SrcWks.Range("A2")
          Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
          If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = SrcWks.Range(Rng, RngEnd)
          
            For Each Cell In Rng
              StockItem = Trim(Cell.Value)
              If StockItem <> "" Then
                If Not StockItems.Exists(StockItem) Then
                  'Add StockItem - First time
                   ReDim QtyData(1)
                     QtyData(0) = Cell.Offset(0, 1).Value
                     QtyData(1) = Cell.Offset(0, 2).Value
                   StockItems.Add StockItem, QtyData
                Else
                  'Accummulate Quantities
                   QtyData = StockItems(StockItem)
                     QtyData(0) = QtyData(0) + Cell.Offset(0, 1).Value
                     QtyData(1) = QtyData(1) + Cell.Offset(0, 2).Value
                   StockItems(StockItem) = QtyData
                End If
              End If
            Next Cell
                    
           'Clear the Summary sheet except for the header row
            Set Rng = DstWks.Range("A2:C2")
            Set RngEnd = DstWks.Cells(Rows.Count, Rng.Column).End(xlUp)
              If RngEnd.Row >= Rng.Row Then Set Rng = DstWks.Range(Rng, RngEnd)
            Rng.ClearContents
            
           'Starting row on Summary sheet
            R = Rng.Row
            
            For Each StockItem In StockItems.Keys
             'List StockITem and Quantities on the Sunnary sheet
              QtyData = StockItems(StockItem)
                DstWks.Cells(R, "A") = StockItem
                DstWks.Cells(R, "B").Resize(1, 2).Value = QtyData
              R = R + 1
            Next StockItem
        
       'Sort the Summary sheet in ascending order
        Rng.Sort Key1:=Rng.Cells(1, 1), Order1:=xlAscending, Header:=xlNo, _
                 MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
                
       'Release the object and memory
        Set StockItems = Nothing
        
    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!)

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Best function to sum quantities associated with text values?

    Alternatively - use a Pivot Table (see the link in sig. for general intro.)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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