+ Reply to Thread
Results 1 to 7 of 7

Macro code to filter and sum unique matched pairs of data

Hybrid View

  1. #1
    Registered User
    Join Date
    06-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro code to filter and sum unique matched pairs of data

    Hi all,

    I am new here and have been stuck with an issue that I've been trying to solve for the past day or so (I'm not too handy with VBA).

    Basically, I have one column of text elements and one column of numbers that is paired to the text to the left in the row. There are several duplicates, so I would basically like to consolidate the data. For example:
    Column 1 - Column 2
    AA - 2
    AA - 3
    AA - 4
    B - 1
    B - 2
    B - 1
    C - 5

    To look like
    AA - 9
    B - 4
    C - 5

    I attached an excel document that is a little more clear. I've also researched using pivot tables, but I would really like a macro. Ideally, the user could select the first column, then select the second column, and the macro would spit out the consolidated data.

    I tried using the record feature as well, but I'm not sure how to make the arrays/macro dynamic (if that makes sense).

    Any help is greatly appreciated.

    Thanks.
    Attached Files Attached Files
    Last edited by mtg5j; 06-18-2010 at 02:14 PM. Reason: solved

  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: Macro code to filter and sum unique matched pairs of data

    I have a ready-to-wear macro to merge numeric values in one column based on matching text values in another column.
    _________________
    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
    Registered User
    Join Date
    06-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro code to filter and sum unique matched pairs of data

    Thanks for the help Jerry. The part 4 code:
    Sub MergeItems()
    'Merge QTY column for same items
    Dim LastRow As Long, Rw As Long
    Dim DelRNG As Range
    Application.ScreenUpdating = False
    
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Set DelRNG = Range("A" & LastRow + 10)
    
    For Rw = 2 To LastRow
        If Application.WorksheetFunction.CountIf(Range("A2:A" & Rw), _
            Range("A" & Rw)) > 1 Then
                Set DelRNG = Union(DelRNG, Range("A" & Rw))
        Else
            Range("B" & Rw) = Application.WorksheetFunction.SumIf(Range("A:A"), _
                Range("A" & Rw), Range("B:B"))
        End If
    Next Rw
    
    DelRNG.EntireRow.Delete xlShiftUp
    Set DelRNG = Nothing
    Application.ScreenUpdating = True
    End Sub
    Seemed to do the trick for what I am doing. However, when I tried using it for data in which column A was a combination of numbers and tex (i.e. EE.1985.DOG), the quantity column had all "0"'s (column A merged successfully).

    Outside of that, I had a couple of other questions too.

    If I wanted the original data to remain and not be displaced by the new data generated by the subroutine macro (the new stuff appears a column or two to the right, for example), how would I approach that?

    Also, what if the data was not in columns A and B, like for example the user had to select the first column and then select the second column?

    Thanks for the help, I'm pretty new to VBA and have only really done some basic stuff with functions. I appreciate it.

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

    Re: Macro code to filter and sum unique matched pairs of data

    1) Major rewrite for the "pick a column" stuff.

    2) Simplest and "ready to use"....Copy the data to another sheet before running the macro so it doesn't affect your current data.

    If the existing macro is misbehaving on your data (the column A values are text, even if part of the value is numeric), then post up a good example workbook showing the misbehaving data and your installed macro. We can look at it together.


    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.

  5. #5
    Registered User
    Join Date
    06-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro code to filter and sum unique matched pairs of data

    Thanks, I got the code to work. Turns out it was a small issue with how the data had been entered. I think the code will work fine for my purposes, but I feel like I should try something on my own so I've been working with something to try and achieve the purposes mentioned in my first post.

    Function Unique2(CodesInput, HoursInput)
    
    Dim Unique() As Variant
    Dim Element As Variant
    Dim i As Integer
    Dim FoundMatch As Boolean
    Dim HoursArray()
    Dim NUnique As Variant
    Dim t As Integer
    
    numunique = 0
    For Each Element In CodesInput
            FoundMatch = False
            'Check to see if item already added
            For i = 1 To numunique
                If Element = Unique(i) Then
                FoundMatch = True
                    Exit For
                End If
            Next i
        
        'Add item to unique list if unique
        If Not FoundMatch And Not IsEmpty(Element) Then
          numunique = numunique + 1
          ReDim Preserve Unique(numunique)
          Unique(numunique) = Element
        End If
        
    Next Element
    
    'Transpose unique elements to vertical list
    temp = WorksheetFunction.Transpose(Unique)
    
    'Sum up the values for unique elements
    For t = 1 To numunique
        tempsum = WorksheetFunction.SumIf(CodesInput, Unique(t), HoursInput)
    Next t
    
    Unique2 = tempsum
    
    End Function
    I'm running into problems with the sum part. I can only return the last element's sum instead of an entire array. If I can return an array that gives me the sum for the unique elements then I will have both:
    -An array of the unique elements
    -An array of the unique sums of those elements

    I figure if I can put them together then I will have a code that works similar to the sub macro, but allows the user to select the inputs. But alas, I am hitting another roadblock.

    Any help is appreciated, and thanks in advance.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    06-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro code to filter and sum unique matched pairs of data

    Small update: I'm now having a problem combining the two arrays in my code.
    Function Unique2(CodesInput, HoursInput)
    
    Dim Unique() As Variant
    Dim Element As Variant
    Dim FoundMatch As Boolean
    Dim HoursArray()
    
    numunique = 0
    For Each Element In CodesInput
            FoundMatch = False
            'Check to see if item already added
            For i = 1 To numunique
                If Element = Unique(i) Then
                FoundMatch = True
                    Exit For
                End If
            Next i
        
        'Add item to unique list if unique
        If Not FoundMatch And Not IsEmpty(Element) Then
          numunique = numunique + 1
          ReDim Preserve Unique(numunique)
          Unique(numunique) = Element
        End If
        
    Next Element
    
    'Transpose unique elements to vertical list
    temp = WorksheetFunction.Transpose(Unique)
    
    'Sum up the values for unique elements
    ReDim HoursArray(1 To numunique)
    For t = 1 To numunique
        tempsum = WorksheetFunction.SumIf(CodesInput, Unique(t), HoursInput)
        HoursArray(t) = tempsum
    Next t
    
    'Transpose unique sum of hours array to vertical
    temp2 = WorksheetFunction.Transpose(HoursArray)
    
    'Combine element array with hours array
    ???????
    
    
    End Function
    I'm tring to return a X by 2 column array, with the first column containing the values from the temp array and the second column containing the values from the temp2 array. I just want them to show up side by side in excel after running this function, but can't figure out a way to do so.

    Any help is appreciated for this beginner.

  7. #7
    Registered User
    Join Date
    06-16-2010
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro code to filter and sum unique matched pairs of data

    Another update:

    I'm having trouble with the union function. I just want the two arrays created in my code to appear next to each other in the output, but putting in union() creates a type mismatch error.
    Function Unique2(codesinput As Range, hoursinput As Range)
    
    Dim Unique()
    Dim Element
    Dim FoundMatch
    Dim HoursArray()
    
    numunique = 0
    For Each Element In codesinput
            FoundMatch = False
            'Check to see if item already added
            For i = 1 To numunique
                If Element = Unique(i) Then
                FoundMatch = True
                    Exit For
                End If
            Next i
        
        'Add item to unique list if unique
        If Not FoundMatch And Not IsEmpty(Element) Then
          numunique = numunique + 1
          ReDim Preserve Unique(numunique)
          Unique(numunique) = Element
        End If
        
    Next Element
    
    'Transpose unique elements to vertical list
    temp = WorksheetFunction.Transpose(Unique)
    
    
    'Sum up the values for unique elements
    ReDim HoursArray(1 To numunique)
    For t = 1 To numunique
        tempsum = WorksheetFunction.SumIf(codesinput, Unique(t), hoursinput)
        HoursArray(t) = tempsum
        Next t
    
    'Transpose unique sum of hours array to vertical
    Dim temp2
    temp2 = WorksheetFunction.Transpose(HoursArray)
    
    
    'Combine element array with hours array
    Unique2 = Union(Unique, HoursArray)
    
    
    End Function
    Is there a way I can get around this?

+ 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