Grouping of lines having the same value in columns "B" and "C" with respect to the following line.

harzer

Board Regular
Joined
Dec 15, 2021
Messages
122
Office Version
  1. 2016
Platform
  1. Windows
Hello, As I am not a vba expert, I am posting my request hoping that an expert will help me find a solution to my problem. In the "B_D" sheet, I have a table of ten columns and many (many) rows whose length varies each year, I would like to group all the rows whose contents of the cell of the 2nd column and that of the cell of the 3rd column are identical with the cells of the following line to the same columns. I prefer (if possible) the use of dictionary(s) given their efficiency in the processing of important data. I add that it would also be desirable (for more visibility) to insert an empty line between each group of data. Sample Data from sheet "B_D"
B_D.jpg

Desired results, see sheet "Result"
Resultat.jpg

I remain at your disposal for any additional information.
Cheers.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
It's very difficult when you don't provide a sample of your data using the XL2BB add in, as very few people would be prepared to copy data from an image. The following code makes the following assumptions:
1. Row 1 is a header row, and your data starts in row 2
2. In your image, "Jeunes" is in cell A1
3. Your data is contiguous

Not sure why you wanted to use a dictionary method (which is more suited to unique values) but see how you go with this.
VBA Code:
Sub harzer1()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("B_D")
    
    'Sort the data by columns B & C
    With ws.Range("A1").CurrentRegion
        .Sort Key1:=ws.Range("B1"), order1:=xlAscending, _
        Key2:=ws.Range("C1"), order2:=xlAscending, Header:=xlYes
    End With
    
    'Determine where groups end
    Dim LRow As Long, LCol As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    Dim ArrIn, ArrOut
    ArrIn = ws.Range(ws.Cells(2, 2), ws.Cells(LRow, 3))
    ReDim ArrOut(1 To UBound(ArrIn), 1 To 1)
    
    For i = 2 To UBound(ArrIn)
        If ArrIn(i, 1) <> ArrIn(i - 1, 1) Or ArrIn(i, 2) <> ArrIn(i - 1, 2) Then ArrOut(i, 1) = 1
    Next i
    ws.Cells(2, LCol).Resize(UBound(ArrIn)) = ArrOut
    
    'Insert the gaps between groups
    Dim r As Range
    Set r = ws.Columns(LCol).SpecialCells(xlCellTypeConstants)
    r.EntireRow.Insert shift:=xlDown
    ws.Columns(LCol).ClearContents
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello,
Thank you for your reply. As you indicated :
1. Line 1 is a header line. = Yes
2. My data starts at line 2. = Yes
3. The "young people" cell is in A1. = Yes
4. My data is contiguous. = Yes
I have done some tests but the results are not correct, the grouped groups are not correct and sometimes I have several blank lines instead of one to separate the groups.
That's why I'll try to send you the data by XL2BB hoping it will work, so you can test yourself.
So here's the data!
Cordially.

Harzer_v02.xls
ABCDEFGHIJ
1JEUNEPÈREMÈREEleveurAgeVolièreCageNé(e)ToursInformation
20811-109/2008 M0811-006/2007 M0811-098/2007 F123456Pas d'infos
30811-021/2008 M0811-007/2007 M0811-047/2006 F123456Pas d'infos
40811-015/2008 F0811-009/2007 M0811-075/2006 F123456Pas d'infos
5HTY03-062/2010 M0811-009/2007 M0811-075/2006 F123456Pas d'infos
60811-006/2010 M0811-016/2009 M0811-133/2008 F123456Pas d'infos
7GUS14-051/2010 F0811-021/2008 M0811-044/2005 F123456Pas d'infos
8GUS14-025/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
9GUS14-039/2010 M0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
10GUS14-041/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
110811-076/2006 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
120811-012/2009 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
130811-153/2009 M0811-076/2006 M0811-018/2008 F123456Pas d'infos
140811-133/2008 F0811-076/2006 M0811-048/2007 F123456Pas d'infos
150811-016/2009 M0811-076/2006 M0811-153/2007 F123456Pas d'infos
16ABD27-054/2010 F0811-104/2009 MABD27-002/2009 F123456Pas d'infos
17ABD27-101/2009 M0811-104/2009 MABD27-002/2009 F123456Pas d'infos
18ABD27-046/2010 F0811-109/2008 M0811-015/2008 F123456Pas d'infos
19ABD27-002/2009 F0811-109/2008 M0811-038/2008 F123456Pas d'infos
20ABD27-088/2008 M0811-134/2007 M0811-094/2005 F123456Pas d'infos
210811-041/2011 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
22ABD27-048/2010 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
23ABD27-012/2014 F0811-153/2009 M0811-006/2010 F123456Pas d'infos
24ABD27-013/2014 M1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
25ABD27-014/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
26ABD27-015/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
27ABD27-016/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
28ABD27-048/2014 M1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
29ABD27-049/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
30ABD27-050/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
31ABD27-051/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
32ABD27-020/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
33ABD27-021/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
34ABD27-022/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
B_D
 
Last edited:
Upvote 0
Unless I'm mistaken, here are the desired results.



Harzer_v02.xls
ABCDEFGHIJ
1JEUNEPÈREMÈREEleveurAgeVolièreCageNé(e)ToursInformation
20811-109/2008 M0811-006/2007 M0811-098/2007 F123456Pas d'infos
3
40811-021/2008 M0811-007/2007 M0811-047/2006 F123456Pas d'infos
5
60811-015/2008 F0811-009/2007 M0811-075/2006 F123456Pas d'infos
7HTY03-062/2010 M0811-009/2007 M0811-075/2006 F123456Pas d'infos
8
90811-006/2010 M0811-016/2009 M0811-133/2008 F123456Pas d'infos
10
11GUS14-051/2010 F0811-021/2008 M0811-044/2005 F123456Pas d'infos
12
13GUS14-025/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
14GUS14-039/2010 M0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
15GUS14-041/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
16
170811-076/2006 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
180811-012/2009 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
19
200811-153/2009 M0811-076/2006 M0811-018/2008 F123456Pas d'infos
21
220811-133/2008 F0811-076/2006 M0811-048/2007 F123456Pas d'infos
23
240811-016/2009 M0811-076/2006 M0811-153/2007 F123456Pas d'infos
25
26ABD27-054/2010 F0811-104/2009 MABD27-002/2009 F123456Pas d'infos
27ABD27-101/2009 M0811-104/2009 MABD27-002/2009 F123456Pas d'infos
28
29ABD27-046/2010 F0811-109/2008 M0811-015/2008 F123456Pas d'infos
30
31ABD27-002/2009 F0811-109/2008 M0811-038/2008 F123456Pas d'infos
32
33ABD27-088/2008 M0811-134/2007 M0811-094/2005 F123456Pas d'infos
34
350811-041/2011 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
36ABD27-048/2010 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
37ABD27-012/2014 F0811-153/2009 M0811-006/2010 F123456Pas d'infos
38
39ABD27-013/2014 M1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
40ABD27-014/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
41ABD27-015/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
42ABD27-016/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
43
44ABD27-048/2014 M1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
45ABD27-049/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
46ABD27-050/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
47ABD27-051/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
48
49ABD27-020/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
50ABD27-021/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
51ABD27-022/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
B_D
 
Upvote 0
Thank you for providing the XL2BB sample - it makes life much easier :)
Please try the adjusted code below, the result of which is at bottom.
VBA Code:
Option Explicit
Sub harzer2()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("B_D")
    
    'Sort the data by columns B & C
    With ws.Range("A1").CurrentRegion
        .Sort Key1:=ws.Range("B1"), order1:=xlAscending, _
        Key2:=ws.Range("C1"), order2:=xlAscending, Header:=xlYes
    End With
    
    'Determine where groups end & insert gaps
    Dim LRow As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    For i = LRow To 3 Step -1
        If ws.Cells(i, 2) <> ws.Cells(i - 1, 2) Or ws.Cells(i, 3) <> ws.Cells(i - 1, 3) Then
            ws.Rows(i).EntireRow.Insert shift:=xlDown
        End If
    Next i
        
    Application.ScreenUpdating = True
End Sub

harzer.xlsm
ABCDEFGHIJ
1JEUNEPÈREMÈREEleveurAgeVolièreCageNé(e)ToursInformation
20811-109/2008 M0811-006/2007 M0811-098/2007 F123456Pas d'infos
3
40811-021/2008 M0811-007/2007 M0811-047/2006 F123456Pas d'infos
5
60811-015/2008 F0811-009/2007 M0811-075/2006 F123456Pas d'infos
7HTY03-062/2010 M0811-009/2007 M0811-075/2006 F123456Pas d'infos
8
90811-006/2010 M0811-016/2009 M0811-133/2008 F123456Pas d'infos
10
11GUS14-051/2010 F0811-021/2008 M0811-044/2005 F123456Pas d'infos
12
13GUS14-025/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
14GUS14-039/2010 M0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
15GUS14-041/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
16
170811-076/2006 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
180811-012/2009 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
19
200811-153/2009 M0811-076/2006 M0811-018/2008 F123456Pas d'infos
21
220811-133/2008 F0811-076/2006 M0811-048/2007 F123456Pas d'infos
23
240811-016/2009 M0811-076/2006 M0811-153/2007 F123456Pas d'infos
25
26ABD27-054/2010 F0811-104/2009 MABD27-002/2009 F123456Pas d'infos
27ABD27-101/2009 M0811-104/2009 MABD27-002/2009 F123456Pas d'infos
28
29ABD27-046/2010 F0811-109/2008 M0811-015/2008 F123456Pas d'infos
30
31ABD27-002/2009 F0811-109/2008 M0811-038/2008 F123456Pas d'infos
32
33ABD27-088/2008 M0811-134/2007 M0811-094/2005 F123456Pas d'infos
34
350811-041/2011 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
36ABD27-048/2010 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
37ABD27-012/2014 F0811-153/2009 M0811-006/2010 F123456Pas d'infos
38
39ABD27-013/2014 M1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
40ABD27-014/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
41ABD27-015/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
42ABD27-016/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
43
44ABD27-048/2014 M1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
45ABD27-049/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
46ABD27-050/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
47ABD27-051/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
48
49ABD27-020/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
50ABD27-021/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
51ABD27-022/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
B_D
 
Upvote 0
Thank you for your reply.
What a pleasure and respect to you for sharing your knowledge and especially for your availability.
I tested your proposal, it gives the right result and meets my expectations.
As this problem is solved, I allow myself to ask you for a proposal in the same order, but this time it is for:

• Via a 2nd button, group all the lines whose content of the cell of the 2nd column is identical with the cells of the following lines in the same column, so we are only working on a single column "B", do not forget S-V-P the dividing line between groups.

• Via a 3rd button, group all the lines whose content of the cell of the 3rd column is identical with the cells of the following lines in the same column, so we are only working on a single column "C", do not forget S-V-P the dividing line between groups.

Thank you in advance for your future proposal.

Unless I'm mistaken, this is the desired result:

Harzer_v02.xls
ABCDEFGHIJ
1JEUNEPÈREMÈREEleveurAgeVolièreCageNé(e)ToursInformation
20811-109/2008 M0811-006/2007 M0811-098/2007 F123456Pas d'infos
3
40811-021/2008 M0811-007/2007 M0811-047/2006 F123456Pas d'infos
5
60811-015/2008 F0811-009/2007 M0811-075/2006 F123456Pas d'infos
7HTY03-062/2010 M0811-009/2007 M0811-075/2006 F123456Pas d'infos
8
90811-006/2010 M0811-016/2009 M0811-133/2008 F123456Pas d'infos
10
11GUS14-051/2010 F0811-021/2008 M0811-044/2005 F123456Pas d'infos
12GUS14-025/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
13GUS14-039/2010 M0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
14GUS14-041/2010 F0811-021/2008 MGUS14-017/2009 F123456Pas d'infos
15
160811-076/2006 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
170811-012/2009 M0811-055/2005 M0811-044/2005 F123456Pas d'infos
18
190811-153/2009 M0811-076/2006 M0811-018/2008 F123456Pas d'infos
200811-133/2008 F0811-076/2006 M0811-048/2007 F123456Pas d'infos
210811-016/2009 M0811-076/2006 M0811-153/2007 F123456Pas d'infos
22
23ABD27-054/2010 F0811-104/2009 MABD27-002/2009 F123456Pas d'infos
24ABD27-101/2009 M0811-104/2009 MABD27-002/2009 F123456Pas d'infos
25
26ABD27-046/2010 F0811-109/2008 M0811-015/2008 F123456Pas d'infos
27ABD27-002/2009 F0811-109/2008 M0811-038/2008 F123456Pas d'infos
28
29ABD27-088/2008 M0811-134/2007 M0811-094/2005 F123456Pas d'infos
30
310811-041/2011 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
32ABD27-048/2010 M0811-153/2009 M0811-006/2010 F123456Pas d'infos
33ABD27-012/2014 F0811-153/2009 M0811-006/2010 F123456Pas d'infos
34
35ABD27-013/2014 M1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
36ABD27-014/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
37ABD27-015/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
38ABD27-016/2014 F1183-028/2012 MGUS14-031/2012 F123456Pas d'infos
39
40ABD27-048/2014 M1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
41ABD27-049/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
42ABD27-050/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
43ABD27-051/2014 F1183-036/2012 MGUS14-118/2012 F123456Pas d'infos
44
45ABD27-020/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
46ABD27-021/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
47ABD27-022/2014 M1183-037/2012 MGUS14-020/2012 F123456Pas d'infos
B_D
 
Upvote 0
Relatively straightforward, but I'll be unavailable for a few hours although I will get to this today.
 
Upvote 0
OK, in essence you're looking for 3 separate subs. The 3 below cover your requirements for columns B & C, column B only, and column C only. Their names tell you which ones they are.
VBA Code:
Sub Group_Both_B_and_C()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("B_D")
    
    'Sort the data by columns B & C
    With ws.UsedRange
        .Sort Key1:=ws.Range("B1"), order1:=xlAscending, _
        Key2:=ws.Range("C1"), order2:=xlAscending, Header:=xlYes
    End With
    
    'Determine where groups end & insert gaps
    Dim LRow As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    For i = LRow To 3 Step -1
        If ws.Cells(i, 2) <> ws.Cells(i - 1, 2) Or ws.Cells(i, 3) <> ws.Cells(i - 1, 3) Then
            ws.Rows(i).EntireRow.Insert shift:=xlDown
        End If
    Next i
        
    Application.ScreenUpdating = True
End Sub

VBA Code:
Sub Group_By_B()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("B_D")
    
    'Sort the data by columns B & C
    With ws.UsedRange
        .Sort Key1:=ws.Range("B1"), order1:=xlAscending, _
        Key2:=ws.Range("C1"), order2:=xlAscending, Header:=xlYes
    End With
    
    'Determine where groups end & insert gaps
    Dim LRow As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    For i = LRow To 3 Step -1
        If ws.Cells(i, 2) <> ws.Cells(i - 1, 2) Then
            ws.Rows(i).EntireRow.Insert shift:=xlDown
        End If
    Next i
        
    Application.ScreenUpdating = True
End Sub

VBA Code:
Sub Group_By_C()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("B_D")
    
    'Sort the data by columns B & C
    With ws.UsedRange
        .Sort Key1:=ws.Range("B1"), order1:=xlAscending, _
        Key2:=ws.Range("C1"), order2:=xlAscending, Header:=xlYes
    End With
    
    'Determine where groups end & insert gaps
    Dim LRow As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    For i = LRow To 3 Step -1
        If ws.Cells(i, 3) <> ws.Cells(i - 1, 3) Then
            ws.Rows(i).EntireRow.Insert shift:=xlDown
        End If
    Next i
        
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hello Kevin9999, Thank you for your proposals which give me total satisfaction.
Sincerely.
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,320
Members
448,887
Latest member
AirOliver

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top