Group rows for sort

zoey2

New Member
Joined
Dec 5, 2017
Messages
11
have an excel sheet
idnumberdescription
123777first row
1226382second row
second row more detail
12472920third row
12038392forth row

<tbody>
</tbody>

And I want to be able to sort the sheet but second row and second row more detail need to stay together so if I sorted by id I would get

idnumberdescription
12038392forth row
1226382second row
second row more detail

<tbody>
</tbody>

etc.. (the end of my table disappeared but it keep sorting on id


Is there a way to link rows together so when you sort they always stick together?
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
This code will put the ID in the more detail row from the row above so that the data can be sorted it will then sort and then remove the added IDs.


try this on a copy of your data.

Code is assuming ID is in column A with row headers
Code:
Sub sortwithblank()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lr
    If Cells(x, 1) = "" Then Cells(x, 1) = Cells(x - 1, 1)
Next x
    
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet4").Sort
        .SetRange Range("A2:C" & lr)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
For y = 2 To lr
    If Cells(y, 2) = "" Then Cells(y, 1).ClearContents
Next y

End Sub
 
Upvote 0
I need something so when sort button on the excel page is used it sticks the rows together.
 
Upvote 0
I am not aware of a way to do what you want. Maybe someone else can come up with something.
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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