Hello efab,
It seems a working example of the code is what you want. I have added a button to the worksheet to run the macro. The data will be sorted by Team, School, and Gender in ascending order. Separating borders will then be added where the numbers change. Here is the macro code...
Sub SortAndFormat()
Dim I As Integer
Dim PrevNum As Integer
Dim Rng As Range
Application.ScreenUpdating = False
Set Rng = ActiveSheet.Cells(1, 1).CurrentRegion
'Sort the table by:Team, School, Gender
Rng.Sort Key1:=Rng.Cells(1, 8), Order1:=xlAscending, _
Key2:=Rng.Cells(1, 7), Order2:=xlAscending, _
Key3:=Rng.Cells(1, 5), Order3:=xlAscending, _
Header:=xlYes, Orientation:=xlSortColumns, MatchCase:=False
'Format only the table data and not the header row
Set Rng = Rng.Offset(1, 0).Resize(Rowsize:=Rng.Rows.Count - 1)
'Remove the separating borders in the table
For I = 1 To Rng.Rows.Count - 1
With Rng.Rows(I).Borders(xlEdgeBottom)
If .LineStyle <> xlLineStyleNone Then
.LineStyle = xlLineStyleNone
End If
End With
Next I
'Add the separating borders between the nunmbers
PrevNum = Rng.Cells(1, 8)
For I = 1 To Rng.Rows.Count - 1
If Rng.Cells(I, 8) <> PrevNum Then
With Rng.Rows(I - 1).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
End If
PrevNum = Rng.Cells(I, 8)
Next I
Application.ScreenUpdating = True
End Sub
Sincerely,
Leith Ross
Bookmarks