+ Reply to Thread
Results 1 to 19 of 19

Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Hi guys,

    I am trying to get a macro to run that will aggregate Rows of data based on if 2 columns contain the same value. I know the best way is to use Pivot Tables, but I absolutely can't use a pivot table, this data needs to be aggregated no pivot table as other macros will import it somewhere. Fyi real spreadsheet has 1000's of rows so can't do manually.

    I have attached a spreadsheet for your reference.

    Basically, the conditions to aggregate the data is:

    If Column I & Column W are the same values as another Rows Column I & W, I would like to aggregate those rows to one line, while doing so summing K (the only value that changes among the Rows).

    The output should give this result in 3 lines under the headers. (aggregating John's order and Abe's order)

    Customer Booked {...} all other data is the same copy paste
    JOHN1234 2 {...}
    ALAIN1234 5 {...}
    ABE1234 6 {...}

    Would really appreciate any help I can get, been at this for a couple days. Everything I can find directs me to Pivot tables but I can't do it that way. Thank you!
    Last edited by AliGW; 10-27-2018 at 02:41 AM. Reason: Spreadsheet remoived as requested by OP - sensitive data therein.

  2. #2
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Hi popcorn7

    JOHN1234 2 {...}
    ALAIN1234 5 {...}
    ABE1234 6 {...}
    This seems incorrect...The red part ...Rows 4 to 7 have same values in F & W
    Or must it be I & W
    Last edited by Sintek; 09-26-2018 at 12:13 PM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Yes apologies, it is indeed column "I" that I am going by. I will edit post. Any help is greatly appreciated

  4. #4
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Perhaps this...
    Results as follows...
    JOHN1234 2
    ALAIN1234 4
    ABE1234 8
    Option Explicit
    
    Sub popcorn7()
    Dim val, i As Long, sr As Long, lr As Long, numrows As Long
    Application.ScreenUpdating = False
    With Sheet1
        With .Cells(1).CurrentRegion
            .Range(.Cells(2, 9), .Cells(.Count, 9)).AdvancedFilter xlFilterCopy, , .Range("AH1"), True
        End With
        With .Range("AH1").CurrentRegion:  val = .Value:  .Clear:  End With
        For i = 2 To UBound(val)
            With .Cells(1).CurrentRegion
                .AutoFilter 9, val(i, 1)
                sr = .Range("F2", .Range("F" & Rows.Count).End(xlUp)).SpecialCells(12).Row
                .AutoFilter 23, .Range("W" & sr)
            End With
            numrows = .AutoFilter.Range.Columns(1).SpecialCells(12).Cells.Count - 1
            If numrows > 1 Then
                lr = .Range("F" & Rows.Count).End(xlUp).Row
                .Range("K" & sr) = Application.Subtotal(9, Range("K:K"))
                .Range("K" & sr + 1 & ":K" & lr).EntireRow.Delete
            End If
            .AutoFilterMode = False
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 09-26-2018 at 01:53 PM.

  5. #5
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    wow that's a very impressive code, thank you so much!

  6. #6
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Only if it works with your actual dataset...
    Thanks.gif
    Last edited by Sintek; 09-26-2018 at 02:50 PM.

  7. #7
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Hey sintek, my actual dataset the Sheet is named "Database" when I change Sheet1 in your code to Database it doesn't work. I also tried writting it Sheets("Database"). I don't see how I can get it modified can you kindly help me out - this should be straight forward.

  8. #8
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    can you kindly help me out
    Sure I can ... if I had a file to work with...

  9. #9
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Yes apologies. It is back. Really appreciate it sintek

  10. #10
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Code works fine...
    With Sheets("Database")
    See result after code ran in upload...

    Edit...slight change to code...

    Option Explicit
    
    Sub popcorn7()
    Dim val, i As Long, sr As Long, lr As Long, numrows As Long, ws As Worksheet
    Application.ScreenUpdating = False
    Set ws = Sheets("Database")
    With ws
        With .Cells(1).CurrentRegion: .Range(.Cells(2, 9), .Cells(.Count, 9)).AdvancedFilter xlFilterCopy, , .Range("AH1"), True: End With
        With .Range("AH1").CurrentRegion:  val = .Value:  .Clear:  End With
        For i = 2 To UBound(val)
            With .Cells(1).CurrentRegion
                .AutoFilter 9, val(i, 1)
                sr = ws.Range("F2", ws.Range("F" & Rows.Count).End(xlUp)).SpecialCells(12).Row
                .AutoFilter 23, ws.Range("W" & sr)
            End With
            numrows = .AutoFilter.Range.Columns(1).SpecialCells(12).Cells.Count - 1
            If numrows > 1 Then
                lr = .Range("F" & Rows.Count).End(xlUp).Row
                .Range("K" & sr) = Application.Subtotal(9, Range("K:K"))
                .Range("K" & sr + 1 & ":K" & lr).EntireRow.Delete
            End If
            .AutoFilterMode = False
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by AliGW; 10-27-2018 at 02:41 AM. Reason: Spreadsheet remoived as requested by OP - sensitive data therein.

  11. #11
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Hi sintek, I haven't answered you because I have been working on this on my actual data set for a day now and I am still having issues applying it. Your method of doing this is really great, I would like to go by it.

    Your code bugs out because when we filter by Client like ALAIN1234, there is in reality many different Order ID's for him, not just one Order ID to aggregate. So I am looking to aggregate each of his orders. I would really appreciate it if you had a look again with this Sample Spreadsheet attached. As it stands it could only be a little edit. I am reaching out to you on last effort, as I said I been at it for a day now and can't get it, my VBA skills aren't your level.

    Little Recap what I am trying to do...Basically I am trying to filter by Col I "Client", and again for Col W, aggregating Col K for each Order ID that is the same.

    Really appreciate your time
    Last edited by popcorn7; 09-27-2018 at 12:20 PM.

  12. #12
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Upload has no ALAIN1234

  13. #13
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    I changed the names, the ALAIN1234 was a desensitized name anyways. We can go with the names there now in the upload anyways, it won't change anything. thank you so much for looking at this, I am too still trying to edit

  14. #14
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    A bit more complex than we both anticipated lol....Try this...

    Option Explicit
    
    Sub popcorn7()
    Dim val, val1, ws As Worksheet
    Dim i As Long, ii As Long, sr As Long, lr As Long, numrows As Long, lrow As Long
    Application.ScreenUpdating = False
    Set ws = Sheets("Daily Input")
    With ws
        lrow = .Cells(Rows.Count, "W").End(xlUp).Row
        Range("A2:AD" & lrow).Sort key1:=Range("I2:I" & lrow), order1:=xlAscending, Header:=xlNo
        With .Cells(1).CurrentRegion: .Range(.Cells(2, 9), .Cells(.Count, 9)).AdvancedFilter xlFilterCopy, , .Range("AH1"), True: End With
        With .Range("AH1").CurrentRegion:  val = .Value:  .Clear:  End With
        For i = 2 To UBound(val)
            .Cells(1).CurrentRegion.AutoFilter 9, val(i, 1)
            sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
            lr = .Range("I" & Rows.Count).End(xlUp).Row
            With .Cells(1).CurrentRegion: .Range(.Cells(sr, 23), .Cells(lr, 23)).SpecialCells(12).AdvancedFilter xlFilterCopy, , .Range("AJ1"), True: End With
            With .Range("AJ1").CurrentRegion:  val1 = .Value:  .Clear:  End With
            .Cells(1).CurrentRegion.AutoFilter 9, val(i, 1)
            For ii = 2 To UBound(val1)
                .Cells(1).CurrentRegion.AutoFilter 23, val1(ii, 1)
                numrows = .AutoFilter.Range.Columns(23).SpecialCells(12).Cells.Count - 1
                If numrows > 1 Then
                    sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
                    lr = .Range("I" & Rows.Count).End(xlUp).Row
                    .Range("K" & sr) = Application.Subtotal(9, Range("K:K"))
                    .Range("K" & sr + 1 & ":K" & lr).EntireRow.Delete
                End If
                .Cells(1).CurrentRegion.AutoFilter 23
            Next ii
        Next i
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 09-28-2018 at 02:22 AM.

  15. #15
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Sintek I can't thank you enough, you are a hero! Really appreciate it

  16. #16
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Hi Sintek,

    View my private msg regarding the updates. We are so close. :O Having an issue when there is a Client with 1 order only. Updated Sample Sheet, problem occurs with TROUBLE1234
    Last edited by popcorn7; 09-28-2018 at 09:05 AM.

  17. #17
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Is it finally PERFECT...

  18. #18
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,380

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Caught me just in time...Away for 2 weeks...

    Option Explicit
    
    Sub popcorn7()
    Dim val, val1, ws As Worksheet
    Dim i As Long, ii As Long, sr As Long, lr As Long, numrows As Long, lrow As Long
    Application.ScreenUpdating = False
    Set ws = Sheets("Daily Input")
    With ws
        lrow = .Cells(Rows.Count, "W").End(xlUp).Row
        Range("A2:AD" & lrow).Sort key1:=Range("I2:I" & lrow), order1:=xlAscending, Header:=xlNo
        With .Cells(1).CurrentRegion: .Range(.Cells(2, 9), .Cells(.Count, 9)).AdvancedFilter xlFilterCopy, , .Range("AH1"), True: End With
        With .Range("AH1").CurrentRegion:  val = .Value:  .Clear:  End With
        For i = 2 To UBound(val)
            .Cells(1).CurrentRegion.AutoFilter 9, val(i, 1)
            numrows = .AutoFilter.Range.Columns(23).SpecialCells(12).Cells.Count - 1
            sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
            lr = .Range("I" & Rows.Count).End(xlUp).Row
            If numrows > 1 Then
                With .Cells(1).CurrentRegion: .Range(.Cells(sr, 23), .Cells(lr, 23)).SpecialCells(12).AdvancedFilter xlFilterCopy, , .Range("AJ1"), True: End With
                With .Range("AJ1").CurrentRegion:  val1 = .Value:  .Clear:  End With
            End If
            For ii = 2 To UBound(val1)
                .Cells(1).CurrentRegion.AutoFilter 23, val1(ii, 1)
                numrows = .AutoFilter.Range.Columns(23).SpecialCells(12).Cells.Count - 1
                If numrows > 1 Then
                    sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
                    lr = .Range("I" & Rows.Count).End(xlUp).Row
                    .Range("K" & sr) = Application.Subtotal(9, Range("K:K"))
                    .Range("K" & sr + 1 & ":K" & lr).EntireRow.Delete
                End If
                .Cells(1).CurrentRegion.AutoFilter 23
            Next ii
        Next i
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub

  19. #19
    Registered User
    Join Date
    09-21-2018
    Location
    Toronto, ON
    MS-Off Ver
    2013
    Posts
    18

    Re: Aggregate Rows of Data if Condition is met (if 2 Columns contains dup values)

    Thnak you so much for your help here, I wish you all the best. Have a great time off/travels All the best to you, can't thank you enough

+ 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. [SOLVED] Loop to scan data and aggregate Rows that are the same
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2018, 06:48 PM
  2. Aggregate function with condition
    By buttonman in forum Excel General
    Replies: 9
    Last Post: 06-30-2017, 02:23 PM
  3. [SOLVED] Aggregate values in columns based on a cell identifier
    By bjarmeister in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-30-2015, 03:33 AM
  4. Aggregate data / remove empty columns
    By patapsco59 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2014, 11:25 AM
  5. Aggregate data in a range of columns to remove blank cells
    By arbgd1 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-05-2013, 04:11 PM
  6. Replies: 3
    Last Post: 11-19-2010, 06:26 AM

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