Excel.Tips.Net ExcelTips (Menu Interface)

Sorting Data Containing Merged Cells

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Sorting Data Containing Merged Cells.

Excel has long included the ability to merge adjacent cells into a larger, single cell. This ability has been used by many worksheet designers to give their worksheets a polished, professional look.

There is a huge drawback to using merged cells, however: You can't sort tables that include them. If you try, you'll get a message that says "The operation requires the merged cells to be identically sized."

The most obvious solution to the problem is to not use merged cells. Let's say, for instance, that you have a worksheet in which each "record" actually consists of two rows, and that the first column of the worksheet contains merged cells. (Each two-row record starts with two merged cells spanning the two rows. This merged cell contains a project name.)

It is better to unmerge the cells in the first column, but then you may wonder how to make the records sort properly in the worksheet; how to keep the row pairs together during a sort. You can do this by putting your project name in the first row and the project name appended with "zz" in the second row. For instance, if the first row contains "Wilburn Chemical" (the project name), then the second row could contain "Wilburn Chemicalzz". Format the second row's cell so the name doesn't show up (such as white text on a white background), and you can then successfully sort as you want to.

Another solution is to use a macro to juggle your worksheet and get the sorting done. Assuming that the merged cells are in column A (as previously described), you can use the following macro to sort the data by the contents of column A:

Sub SortList()
    Dim sAddStart As String
    Dim rng As Range
    Dim rng2 As Range
    Dim lRows As Long

    Application.ScreenUpdating = False
    sAddStart = Selection.Address
    Set rng = Range("A1").CurrentRegion

    With rng
        lRows = .Rows.Count - 1
        .Cells(1).Offset(0, -1) = "Temp"
        .Cells(1).Offset(1, -1).FormulaR1C1 = _
          "=+RC[1]&"" ""&ROW()"
        .Cells(1).Offset(2, -1).FormulaR1C1 = _
          "=+R[-1]C[1]&"" ""&ROW()"
        Set rng2 = .Cells(1).Offset(1, -1).Resize(lRows, 1)
        Range(.Cells(2, 0), .Cells(3, 0)).AutoFill _
        rng2.PasteSpecial Paste:=xlValues

        .Columns(1).MergeCells = False

    .CurrentRegion.Sort _
        Key1:=Range("A2"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, _
        MatchCase:=False, Orientation:=xlTopToBottom


        With Range(.Cells(2, 1), .Cells(3, 1))
            .Cells(3, 1).Resize(lRows - 2, 1). _
                PasteSpecial Paste:=xlFormats
        End With
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

The macro inserts a temporary column, reads the items from the first column of the list, appends the row number, copies it down the temporary column, unmerges the cells, sorts the list, deletes the temporary column, and re-merges column A. (That's a lot of work just to sort a table with merged cells!)

This macro is very specific to a particular layout of your data, and therefore would need to be tested and probably modified to make sure it would work with data formatted in any other way.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2581) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Sorting Data Containing Merged Cells.

Related Tips:

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 4+5 (To prevent automated submissions and spam.)
          Commenting Terms

Comments for this tip:

Muralidhara Rao Bongu    13 Oct 2015, 06:37
I merged 3 coloums into One column, below that I filled with 3 number. how can i Find the values of those three numbers.
Give me suggession to improve MS-Excell 2003
Yashwant Khare    01 Sep 2015, 09:11
How to remove blank & merge cells in sheet with formula or shortcut.
Bill    04 Jun 2015, 17:51
Macros are too complicated for most Excel users, particularly since people have a tendency to change columns, etc once you give them the workbook.

A better solution that I have found is to add two rows below the merged and formatted header rows: one a blank one, and the other one a "new" header row above the dataset.

If rows #1 and #2 are our nicely merged and formatted header rows with a dataset below them, let's add two new rows (#3 and #4) between the original header rows and the dataset.

Leave the #3 row blank. The new #4 row will have the same header row information from rows #1 and #2, but in a single cell per column.

These new rows essentially makes Excel view the dataset below the headers as a different array.

Now make the new rows #4 and #4 a row height = 1.

This will effectively hide these rows from view. Now you have your original header rows with merged cells, a blank row, a new single row header row with the dataset below it.

Now if you click on any cell in the dataset you can use standard sort features of Excel to sort the data and you still have your nicely formatted rows with no messy macros.
Nik    28 Jul 2014, 23:23
Great advice, Allen. Are you able to shortcut me to modifying the macro above to apply to, say, the "L" column opposed to the "A" column? I have been fiddling but I think it will take a long time without having to go through some course on macros...
Tracey    01 Jun 2014, 22:30
Thank you thank you thank you :)
Michael (Micky) Avidan    21 Feb 2014, 08:04
Better late than never.
Check out:
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)

janet    26 Oct 2013, 17:36
I'm using a spreadsheet that I didn't create. How do I find the merged cells?

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2015 Sharon Parq Associates, Inc.