How do I sort with merged cells?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
I have data in A1 through V17.

Row 1 is my header row.

A2:A3 are merged, A4:A5 are merged, and that trend continues up to and including A16:A17

Similarly:
V2:V3 are merged, V4:V5 are merged, and that trend continues up to and including V16:V17

I need to do a descending value sort on column V, but I always want row 2 and 3 to stay together, 4 and 5 and so on up to and including row 16 and 17.

How do I go about sorting this? Any help would be greatly appreciated. Thank you.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The universal answer to the manifold problems of merged cells is to unmerge them.
 
Upvote 0
if you have the cells merged for appearance sake, could you use ALT-ENTER to create a line break within the cells in Column A and V. Then everything would be one row, but still have the appreance you are looking for.
 
Upvote 0
The universal answer to the manifold problems of merged cells is to unmerge them.

Don't think that's an option unfortunately....

if you have the cells merged for appearance sake, could you use ALT-ENTER to create a line break within the cells in Column A and V. Then everything would be one row, but still have the appreance you are looking for.

No, its not for appearance - how do I post the spreadsheet here like I see everyone doing?
 
Upvote 0
Upvote 0
So here I think you can see why I need this:

wmclzb.jpg
 
Upvote 0
Merge cells always seem to cause problems. One thing... you can't sort with them. Here is a macro that you can run to unmerge the cells just before you sort them and then run again to remerge them....

Code:
Sub ToggleMergeForColumnsAandV()
  Dim X As Long, LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Application.ScreenUpdating = False
  For X = 2 To LastRow Step 2
    If Cells(X, "A").MergeCells Then
      Cells(X, "A").MergeArea.UnMerge
      Cells(X + 1, "A").Value = Cells(X, "A").Value
      Cells(X, "V").MergeArea.UnMerge
      Cells(X + 1, "V").Value = Cells(X, "V").Value
    Else
      Application.DisplayAlerts = False
      Cells(X, "A").Resize(2).Merge
      Cells(X, "V").Resize(2).Merge
      Application.DisplayAlerts = True
    End If
  Next
  Application.ScreenUpdating = True
End Sub
One thing with your sort, though... I think you should sort on both Column V and Column A (Column V as primary, Column A as secondary), otherwise there might be a chance for identical values in the unmerged Column V to split the unmerged Teams apart from each other.
 
Upvote 0
Merge cells always seem to cause problems. One thing... you can't sort with them. Here is a macro that you can run to unmerge the cells just before you sort them and then run again to remerge them....

Code:
Sub ToggleMergeForColumnsAandV()
  Dim X As Long, LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Application.ScreenUpdating = False
  For X = 2 To LastRow Step 2
    If Cells(X, "A").MergeCells Then
      Cells(X, "A").MergeArea.UnMerge
      Cells(X + 1, "A").Value = Cells(X, "A").Value
      Cells(X, "V").MergeArea.UnMerge
      Cells(X + 1, "V").Value = Cells(X, "V").Value
    Else
      Application.DisplayAlerts = False
      Cells(X, "A").Resize(2).Merge
      Cells(X, "V").Resize(2).Merge
      Application.DisplayAlerts = True
    End If
  Next
  Application.ScreenUpdating = True
End Sub
One thing with your sort, though... I think you should sort on both Column V and Column A (Column V as primary, Column A as secondary), otherwise there might be a chance for identical values in the unmerged Column V to split the unmerged Teams apart from each other.

I get what that is doing - seems great. Could it go a step further, and run upon entering the sheet to unmerge, then sort, then run again to remerge?

Thanks
 
Upvote 0
Okay, here is the macro restructured to do the unmerge, sort and then merge again (install it in a standard module like you would do for any other macro)...

Code:
Sub UnmergeSortMerge()
  Dim X As Long, LastRow As Long
  Application.ScreenUpdating = False
  ' Unmerge cells
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To LastRow Step 2
    Cells(X, "A").MergeArea.UnMerge
    Cells(X + 1, "A").Value = Cells(X, "A").Value
    Cells(X, "V").MergeArea.UnMerge
    Cells(X + 1, "V").Value = Cells(X, "V").Value
  Next
  ' Sort
  Range("A:V").Sort Key1:=Range("V1"), Order1:=xlAscending, _
                    Key2:=Range("A1"), Order2:=xlAscending, _
                    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
                    DataOption2:=xlSortNormal
  ' Merge cells
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To LastRow Step 2
    Application.DisplayAlerts = False
    Cells(X, "A").Resize(2).Merge
    Cells(X, "V").Resize(2).Merge
    Application.DisplayAlerts = True
  Next
  Application.ScreenUpdating = True
End Sub
As written, you still have to call this from somewhere; meaning, via the ALT+F8 macro dialog box, or you can assign it to a button and use that to call it up, or you can call it from other VB code. You indicated that you wanted this to run automatically "upon entering the sheet". To do that, call up the worksheet's code module and put this in it...

Private Sub Worksheet_Activate()
UnmergeSortMerge
End Sub
 
Upvote 0
Okay, here is the macro restructured to do the unmerge, sort and then merge again (install it in a standard module like you would do for any other macro)...

Code:
Sub UnmergeSortMerge()
  Dim X As Long, LastRow As Long
  Application.ScreenUpdating = False
  ' Unmerge cells
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To LastRow Step 2
    Cells(X, "A").MergeArea.UnMerge
    Cells(X + 1, "A").Value = Cells(X, "A").Value
    Cells(X, "V").MergeArea.UnMerge
    Cells(X + 1, "V").Value = Cells(X, "V").Value
  Next
  ' Sort
  Range("A:V").Sort Key1:=Range("V1"), Order1:=xlAscending, _
                    Key2:=Range("A1"), Order2:=xlAscending, _
                    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
                    DataOption2:=xlSortNormal
  ' Merge cells
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For X = 2 To LastRow Step 2
    Application.DisplayAlerts = False
    Cells(X, "A").Resize(2).Merge
    Cells(X, "V").Resize(2).Merge
    Application.DisplayAlerts = True
  Next
  Application.ScreenUpdating = True
End Sub
As written, you still have to call this from somewhere; meaning, via the ALT+F8 macro dialog box, or you can assign it to a button and use that to call it up, or you can call it from other VB code. You indicated that you wanted this to run automatically "upon entering the sheet". To do that, call up the worksheet's code module and put this in it...

Private Sub Worksheet_Activate()
UnmergeSortMerge
End Sub

I'm quite proud of myself - I almost had that!

Couple of tweaks - made the first sort descending, and just made this macro a worksheet_activate() one, rather than making a separate one that calls it.

Works great. Thanks so much for your help!
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

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