Photo by Mario Purisic on Unsplash
Grouping information in Excel is a way of hiding data, while telling the user there is data hidden. In my opinion it is a preferable way to "hide" information as you are aware that there is data grouped. Hidden rows in the conventional sense are often hard to detect. You may find out by accident after you have copied information that there were rows hidden. It is often a surprise.
The following will group like data if it is in a sequence (sorted first). It works nicely as it hides duplicated data leaving other unique items visible.
Option Explicit
Sub GroupLike()
Dim i As Long
Dim j As Long
j = 0
For i = 2 To Range("A" & Rows.Count).End(xlUp).row
If Cells(i, "A") = Cells(i - 1, "A") Then
Cells(i, "A").EntireRow.Group
If j = 0 Then
Cells(i - 1, "A").EntireRow.Group
j = j + 1
Else: End If
Else
j = 0
End If
Next i
End Sub
I will of course provide a working Excel file to go with the code.