Group rows for sort

zoey2

New Member
Joined
Dec 5, 2017
Messages
11
have an excel sheet[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]id[/TD]
[TD]number[/TD]
[TD]description[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]777[/TD]
[TD]first row[/TD]
[/TR]
[TR]
[TD]122[/TD]
[TD]6382[/TD]
[TD]second row[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]second row more detail[/TD]
[/TR]
[TR]
[TD]124[/TD]
[TD]72920[/TD]
[TD]third row[/TD]
[/TR]
[TR]
[TD]120[/TD]
[TD]38392[/TD]
[TD]forth row
[/TD]
[/TR]
</tbody>[/TABLE]

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

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]id[/TD]
[TD]number[/TD]
[TD]description[/TD]
[/TR]
[TR]
[TD]120[/TD]
[TD]38392[/TD]
[TD]forth row[/TD]
[/TR]
[TR]
[TD]122[/TD]
[TD]6382[/TD]
[TD]second row[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]second row more detail[/TD]
[/TR]
</tbody>[/TABLE]

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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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