Forum Discussion

Rinanachas's avatar
Rinanachas
Copper Contributor
Jun 02, 2021

Sort by merged cell size

Hey everyone,

I am trying to sort data according to their merged cell size. This means I want to have a list with all the single cells first, then all the double merged cells, then triple merged cells etc. from top to bottom.

Is there any way to do this?

Thanks in advance!

 

5 Replies

  • Rajesh_Sinha's avatar
    Rajesh_Sinha
    Iron Contributor

    Rinanachas 

     

    What you are trying is something hypothetical but yes it has a solution also.

     

    • Let me remind that if you are thinking that the Merged cell looking taller has different Height or Excel increases it's Height then you are wrong. 
    • Merged cell's Height is as similar to a normal cell's.

     

    Now how to Sort data in Merged Cells,, has tricky solution.

     

    • Press F5, you get dialogue box. 
    • Hit Special then click Blank.

    You find all blank cells,, are part of Merged cells been selected.

    • Now type a  = sign into the Formula Bar.

    • Write the reference the cell which locates above the first blank cell.

    •  And press Ctrl + Enter button simultaneously.

      You find all blank cells are filled with the value above. 

    • Now select the Column & SORT.

     

    Another is the better and is faster too, is VBA Macro.

     

     

    Private Sub SortMergerdCells()
    
     Dim MyRange As Range
     Set MyRange = Range("A2:A21")
     
     On Error Resume Next
     
     With MyRange
     .UnMerge
     .Resize(.Rows.Count, 1).SpecialCells(xlBlanks).Formula = "=R[-1]C"
     .Sort Key1:=.Cells(1, 1)
     Range("VBA_RecFmt").Copy
     .PasteSpecial Paste:=xlPasteFormats
     End With
     
     Call Duplicate
     
    End Sub
    
    Sub Duplicate()
    Dim a As Long
    For a = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If WorksheetFunction.CountIf(Range("A2:A" & a), Cells(a, 1)) > 1 Then Rows(a).Delete
    Next
    End Sub

     

     

    How it works:

     

    Above are two Macro.

     

    • Either press ALT+F11 or select the Sheet TAB, then Right Click.
    • From Menu hit View Code.

     

    Now VB editor will open.

     

    • Copy & Paste this code (Excel will automatically dived them into two).
    • Now go to Developer TAB, hit Macro & select the Macro from the List and hit RUN.

     

    You find the Data has been Sorted.

     

    N.B.  In first VBA code check this Range("A2:A21") , is data Range has value in both Merged and in Un-Merged cells, and you need to adjust it as needed.

    • Rinanachas's avatar
      Rinanachas
      Copper Contributor
      Thank you, I will try it out and hope I will manage!
      • Rajesh_Sinha's avatar
        Rajesh_Sinha
        Iron Contributor
        Both of my suggested methods are working,, read instruction while using ,, will work ,, if my solution works then you may accept it as Best Answer as well Like & keep asking ☺
  • Rinanachas 

    This is going to sound perverse, but calculation (in this case a sort) is far easier if you avoid merging cells.  The display can be made to look much the same by using conditional formatting to change the font colour to white, or the number format to blank, whenever successive cells contain the same ID.

    By having the userID on every row, it is possible to count the occurrences rather than the size of the merged cell.

    Note: SORTBY requires Excel 365.

    • Rinanachas's avatar
      Rinanachas
      Copper Contributor
      Thank you so much. I will give it a shot and hope it will work! 🙂

Resources