+ Reply to Thread
Results 1 to 5 of 5

Combining rows if 2 values are met

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2021
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    10

    Question Combining rows if 2 values are met

    Hi there, I'm trying to combine multiple rows of answers by the same subject based on an Subject number and a certain text. For example rows 2 through 4 should be combined into a single row as their subject number and text matches, this should result in Subject 123 with the text Hello this is a test, in 2010 with times picked A = 6, B = 2, C= 3 and D = 1 in this row.

    I tried doing this by creating IF formulas, but I could not figure out how to have it automatically loop to keep comparing to the row below. I also tried to write a VBA code, which works for a single row and a single column, but I cannot figure out how to make it check multiple rows and loop automatically.

    Any help is greatly appreciated!
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Combining rows if 2 values are met

    Hi,

    I can't help thinking a Pivot Table isn't the most obvious

    See attached
    Attached Files Attached Files
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Combining rows if 2 values are met

    Try this:
    Private Sub CombineTasks()
    'if year & task name are the same combine the rows; override year with
    'any year found (ignore blanks)and sum the "times picked" values in cols D->G.
    Dim celS As Range, celY As Range, celT As Range, celA As Range, celB As Range, celC As Range, celD As Range
    Dim rngData As Range, celCheck As Range
    Dim lngCurrentRow As Long
    
        'set the data range to search:
        Set celA = Cells(Rows.Count, 1).End(xlUp)
        Set celB = Cells(1, Columns.Count).End(xlToLeft)
        Set rngData = Range(Cells(2, 1), Cells(celA.Row, celB.Column))
        
        For Each celS In rngData.Columns(1).Cells
            If celS.Value <> Empty Then
                Set celY = celS.Offset(0, 1)    'year
                Set celT = celS.Offset(0, 2)    'task text
                Set celA = celS.Offset(0, 3)    'times picked A
                Set celB = celS.Offset(0, 4)    'times picked B
                Set celC = celS.Offset(0, 5)    'times picked C
                Set celD = celS.Offset(0, 6)    'times picked D
                'search for matching subject IDs in col A:
                lngCurrentRow = celS.Row
    BeginSearch:
                Set celCheck = rngData.Columns(1).Find(what:=celS.Value, after:=celS, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False)
                If Not celCheck Is Nothing Then
                    If celCheck.Row <> lngCurrentRow Then
                        'subject ID matches - check task text:
                        If celT.Value = celCheck.Offset(0, 2).Value Then
                            'task text matches - check the year is the same or blank:
                            If celY.Value = celCheck.Offset(0, 1).Value Or celCheck.Offset(0, 1).Value = Empty Then
                                'merge the times picked:
                                celA.Value = celA.Value + celCheck.Offset(0, 3).Value
                                celB.Value = celB.Value + celCheck.Offset(0, 4).Value
                                celC.Value = celC.Value + celCheck.Offset(0, 5).Value
                                celD.Value = celD.Value + celCheck.Offset(0, 6).Value
                                'overwrite the year if blank:
                                If celY.Value = Empty And celCheck.Offset(0, 1).Value <> Empty Then _
                                    celY.Value = celCheck.Offset(0, 1).Value
                                'clear the row:
                                rngData.Rows(celCheck.Row - 1).ClearContents
                                'keep checking for other matches:
                                GoTo BeginSearch
                            End If
                        End If
                    End If
                End If
            End If
        Next celS
        
        'resort the data range:
        With Sheet1.Sort.SortFields
            .Clear
            .Add2 Key:=rngData.Resize(rngData.Rows.Count - 1, 1), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        End With
        With Sheet1.Sort
            .SetRange rngData
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        'purge memory:
        Set rngData = Nothing
        Set celS = Nothing: Set celY = Nothing: Set celT = Nothing
        Set celA = Nothing: Set celB = Nothing: Set celC = Nothing: Set celD = Nothing
        
    End Sub
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  4. #4
    Registered User
    Join Date
    10-05-2021
    Location
    Netherlands
    MS-Off Ver
    2016
    Posts
    10

    Re: Combining rows if 2 values are met

    Quote Originally Posted by MatrixMan View Post
    Try this:
    Private Sub CombineTasks()
    'if year & task name are the same combine the rows; override year with
    'any year found (ignore blanks)and sum the "times picked" values in cols D->G.
    Dim celS As Range, celY As Range, celT As Range, celA As Range, celB As Range, celC As Range, celD As Range
    Dim rngData As Range, celCheck As Range
    Dim lngCurrentRow As Long
    
        'set the data range to search:
        Set celA = Cells(Rows.Count, 1).End(xlUp)
        Set celB = Cells(1, Columns.Count).End(xlToLeft)
        Set rngData = Range(Cells(2, 1), Cells(celA.Row, celB.Column))
        
        For Each celS In rngData.Columns(1).Cells
            If celS.Value <> Empty Then
                Set celY = celS.Offset(0, 1)    'year
                Set celT = celS.Offset(0, 2)    'task text
                Set celA = celS.Offset(0, 3)    'times picked A
                Set celB = celS.Offset(0, 4)    'times picked B
                Set celC = celS.Offset(0, 5)    'times picked C
                Set celD = celS.Offset(0, 6)    'times picked D
                'search for matching subject IDs in col A:
                lngCurrentRow = celS.Row
    BeginSearch:
                Set celCheck = rngData.Columns(1).Find(what:=celS.Value, after:=celS, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False)
                If Not celCheck Is Nothing Then
                    If celCheck.Row <> lngCurrentRow Then
                        'subject ID matches - check task text:
                        If celT.Value = celCheck.Offset(0, 2).Value Then
                            'task text matches - check the year is the same or blank:
                            If celY.Value = celCheck.Offset(0, 1).Value Or celCheck.Offset(0, 1).Value = Empty Then
                                'merge the times picked:
                                celA.Value = celA.Value + celCheck.Offset(0, 3).Value
                                celB.Value = celB.Value + celCheck.Offset(0, 4).Value
                                celC.Value = celC.Value + celCheck.Offset(0, 5).Value
                                celD.Value = celD.Value + celCheck.Offset(0, 6).Value
                                'overwrite the year if blank:
                                If celY.Value = Empty And celCheck.Offset(0, 1).Value <> Empty Then _
                                    celY.Value = celCheck.Offset(0, 1).Value
                                'clear the row:
                                rngData.Rows(celCheck.Row - 1).ClearContents
                                'keep checking for other matches:
                                GoTo BeginSearch
                            End If
                        End If
                    End If
                End If
            End If
        Next celS
        
        'resort the data range:
        With Sheet1.Sort.SortFields
            .Clear
            .Add2 Key:=rngData.Resize(rngData.Rows.Count - 1, 1), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        End With
        With Sheet1.Sort
            .SetRange rngData
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        'purge memory:
        Set rngData = Nothing
        Set celS = Nothing: Set celY = Nothing: Set celT = Nothing
        Set celA = Nothing: Set celB = Nothing: Set celC = Nothing: Set celD = Nothing
        
    End Sub
    Thank you this seems to be working perfectly! In my actual database I use columns A-AB. After expanding from CelD onwards instead of using CelY for year and CelT for the text and CelS can I just use AC, AD, AE?

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Combining rows if 2 values are met

    Quote Originally Posted by losc View Post
    Thank you this seems to be working perfectly! In my actual database I use columns A-AB. After expanding from CelD onwards instead of using CelY for year and CelT for the text and CelS can I just use AC, AD, AE?
    Sorry - I'm not picturing this .. it's fairly easy to expand and if you've managed to get it sorted then great; if not, attach the real file and I'll adjust it for you.

+ 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. Formula for Combining Values from Rows
    By iabhishek in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-05-2020, 11:09 AM
  2. Replies: 5
    Last Post: 07-31-2018, 01:43 PM
  3. Need Help Combining rows and totaling cell values within them
    By Happy Hooker in forum Excel General
    Replies: 1
    Last Post: 01-04-2017, 04:25 PM
  4. Combining some of the rows Values based on multiple criteria (2)
    By Svenbe in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-18-2014, 08:59 AM
  5. Combining Rows Based On Matching Values in one column
    By jeffocasm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2013, 07:16 PM
  6. Combining rows where some cells have null values
    By speechcommgal in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-23-2013, 02:27 PM
  7. Combining Values in rows and keeping unique values (adv. filtering?)
    By nguyeda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2011, 01:48 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