Automatically Merge Duplicate Cells in Excel? What Works

How-to basics · Updated November 18, 2026

Excel has no built-in command that automatically merges cells just because their values match. For repeated values in adjacent cells, a VBA macro can merge matching runs, but merging keeps only the first cell’s content—so use it only when removing the other cells’ contents is intentional. In many worksheets, removing duplicates or grouping the data is safer.

What “merge duplicate cells” can mean

People use “merge” to describe several different spreadsheet tasks. It might mean joining neighboring cells into one larger cell, keeping one copy of each repeated value, combining text from several cells, or grouping records that share a field. These operations have different results, and Excel does not treat them as interchangeable.

Excel’s Merge Cells feature combines a selected range into a single cell. It is mainly a layout tool—for example, to place a heading across several columns. If the selected cells contain values, Excel keeps the upper-left value and removes the contents of the other cells. It does not compare values first or preserve all the repeated entries.

By contrast, removing duplicates deletes repeated rows or values according to the columns you select. Grouping data summarizes or organizes records without turning several worksheet cells into one. Decide which outcome you want before choosing a tool.

Can Excel find and merge matching values automatically?

Excel for Microsoft 365 on Windows can identify duplicates, but its built-in duplicate tools do not automatically merge matching cells. To highlight repeated entries, select the range and choose Home > Conditional Formatting > Highlight Cells Rules > Duplicate Values. This marks duplicates visually; it does not change or combine them.

To remove duplicate records, select the data and choose Data > Remove Duplicates. In the dialog, choose the columns Excel should compare. If you select only a name column, Excel treats matching names as duplicates even if other columns contain different information. If you select every column, rows are removed only when their values match across all selected columns. Make a copy first if the records matter: removal changes the data.

Neither feature merges cells. If you need actual merged cells, Excel’s ribbon command is manual, or you can automate a narrowly defined task with VBA. Excel for the web does not run VBA macros, so a macro-based method requires the desktop app.

When merging adjacent duplicates is appropriate

Merging can be reasonable when repeated labels appear in consecutive rows and the worksheet is meant to be a presentation or printout rather than a working data table. For example, a report might list several products under the same department, and the department name may be displayed once in a vertically merged cell beside those rows.

That appearance comes at a cost. The rows no longer each contain a department value, which can make sorting, filtering, copying, and using PivotTables or formulas more difficult. A merged range is also awkward to navigate and edit because it behaves as one larger cell. In a clean data table, repeating the department name on every row is usually more useful than merging it.

Before merging, check that the matching cells are adjacent and that you really want one visible cell. If identical values appear in separate parts of the sheet, merging each group would require a different rule—and cells separated by other content cannot be combined into one continuous merged range.

Use a VBA macro for a controlled merge

The example below merges consecutive, nonblank cells with equal values in one selected column. Select only the bounded range you want to change, such as A2:A50; do not select the entire worksheet column. The macro compares cell values, not formatting, and skips blanks and error values. It does not merge duplicates separated by a different value.

To add it in desktop Excel for Windows, open the Visual Basic Editor from the Developer tab, choose Insert > Module, and paste the code into the module. If the Developer tab is not visible, turn it on in Excel’s ribbon settings. Save a separate copy of the workbook before running the macro: VBA changes cannot be undone with Ctrl+Z. On a Mac, use Excel’s Developer tab to open Visual Basic; Excel for the web cannot run this macro.

Option Explicit

Sub MergeAdjacentDuplicatesInOneColumn()
    Dim target As Range
    Dim i As Long
    Dim lastRow As Long
    Dim nextRow As Long
    Dim cell As Range

    If TypeName(Selection) <> "Range" Then Exit Sub
    Set target = Selection

    If target.Areas.Count <> 1 Or target.Columns.Count <> 1 Then
        MsgBox "Select one continuous range in one column."
        Exit Sub
    End If

    For Each cell In target.Cells
        If cell.MergeCells Then
            MsgBox "The selection already contains merged cells."
            Exit Sub
        End If
    Next cell

    i = 1
    Do While i <= target.Rows.Count
        If IsError(target.Cells(i, 1).Value2) Or _
           IsEmpty(target.Cells(i, 1).Value2) Or _
           (VarType(target.Cells(i, 1).Value2) = vbString And _
            Len(target.Cells(i, 1).Value2) = 0) Then
            i = i + 1
        Else
            lastRow = i
            nextRow = i + 1

            Do While nextRow <= target.Rows.Count
                If Not SameCellValue(target.Cells(i, 1).Value2, _
                                     target.Cells(nextRow, 1).Value2) Then Exit Do
                lastRow = nextRow
                nextRow = nextRow + 1
            Loop

            If lastRow > i Then
                target.Cells(i, 1).Resize(lastRow - i + 1, 1).Merge
            End If
            i = lastRow + 1
        End If
    Loop
End Sub

Private Function SameCellValue(ByVal firstValue As Variant, _
                               ByVal secondValue As Variant) As Boolean
    If IsError(firstValue) Or IsError(secondValue) Then Exit Function
    If IsEmpty(firstValue) Or IsEmpty(secondValue) Then Exit Function
    If VarType(firstValue) <> VarType(secondValue) Then Exit Function
    SameCellValue = (firstValue = secondValue)
End Function

After selecting the intended range, run MergeAdjacentDuplicatesInOneColumn from the macro list. Review the selection carefully first. The macro merges only vertically adjacent runs; for example, A2:A4 containing “North” three times becomes one merged cell showing “North.” The contents in A3 and A4 are removed by the merge.

What the macro does—and what it does not

The code deliberately limits its scope to one continuous selection in one column. It checks for already merged cells, skips blank and error cells, and compares the underlying values. If the cells contain formulas that return the same result, they may be treated as matching; merging still removes the formulas in the cells other than the first one. A cell’s displayed formatting is not the comparison rule.

It also does not merge values that merely look alike. For example, text with an extra space may be different from text without it, and values with different underlying types are not considered equal by this macro. If a run is interrupted—“North,” “South,” “North”—the two North cells remain separate because they are not adjacent. This avoids guessing whether two distant groups should become one label.

Macros can make broad changes quickly, so use a copy and test on a small sample first. Only run code you understand and trust. If your goal is to combine distinct text values rather than discard repeats, do not use this macro; use a text-combining method instead.

Safer alternatives for data and reports

If you want a list with one instance of each value, a dynamic-array formula can produce that list without modifying the source. With source values in A2:A7—East, East, West, West, West, and North—enter =UNIQUE(A2:A7) in an empty cell. In Excel for Microsoft 365 or Excel 2021, it returns East, West, and North in first-seen order. This creates a distinct list; it does not merge cells or delete the original entries.

For a one-time cleanup, Data > Remove Duplicates is more suitable when you truly want to delete repeated records. Keep a backup and choose comparison columns deliberately. For a report where repeated labels should merely appear once visually, consider leaving the source table intact and building a separate presentation sheet. You can also use borders, indentation, or a PivotTable to clarify groups without merging the underlying data cells.

If the goal is to combine text from multiple cells into one result, use a formula designed to join text, or copy and paste values into a report layout. That is different from Merge Cells: a merge does not concatenate the contents. It retains the top-left cell’s content and discards other selected values.

Power Query and repeated records

When duplicates are part of a recurring import or cleanup process, Power Query may be a better fit than merging. It can transform a table, remove duplicates based on chosen columns, or group rows and calculate summaries. In Excel for Windows, start from Data > From Table/Range to open the Power Query Editor, then use its duplicate-removal or grouping options as appropriate. The exact available commands can depend on the selected column and query context.

Power Query produces a cleaned or summarized table when you load the result; it does not create merged worksheet cells. That distinction is useful: structured rows remain easier to analyze, and you can refresh a query when source data changes. Use it for repeatable data preparation, not for a visual effect that requires a merged heading.

Common problems and checks before merging

  • Data disappears: Excel keeps the upper-left cell’s content and removes the other contents in a merged range. Copy the original values elsewhere if you need them.
  • Sorting or filtering behaves unexpectedly: merged cells can conflict with operations that expect a value in every row. Unmerge them and fill the label down when preparing a table for analysis.
  • Duplicates do not match: leading or trailing spaces, different data types, or hidden differences can make values unequal. Inspect and clean the source before automating a comparison.
  • The range is not contiguous: Merge Cells works on a continuous rectangular selection, and the macro above accepts one continuous column only.
  • The command is unavailable: worksheet protection or the current selection can restrict editing. Check whether the sheet is protected and select a suitable range.

For repeated labels in a data table, a common repair is to unmerge the range and fill the label into each row. That restores a value for every record, making the table easier to sort, filter, and analyze. Reserve merged cells for headings or carefully formatted output.

Frequently Asked Questions

Can Excel automatically merge cells with the same value?

Not with a built-in automatic merge command. Excel can highlight duplicates or remove duplicate records. To merge matching adjacent cells, use a suitable macro or merge the ranges manually, and remember that other cell contents are discarded.

Does Merge Cells combine the text in duplicate cells?

No. Merge Cells keeps the value in the upper-left cell and removes the contents of the other selected cells. To join text, use a text-combination formula or another method that explicitly concatenates the values.

Can I merge duplicates that are not next to each other?

Not into one continuous merged cell when other cells lie between them. You can remove duplicate records or create a distinct list instead. The macro above merges only consecutive matching values in a selected column.

Will merging duplicate cells affect sorting and filtering?

It can. Merged cells do not provide a separate value in each row, which can make a table harder to sort, filter, or analyze. Keep data in unmerged rows and use a separate report layout if you need a more compact appearance.

Can Excel for the web run a VBA merge macro?

No. Excel for the web does not run VBA macros. Open the workbook in the desktop Excel app to use VBA, or choose a built-in alternative such as conditional formatting, Remove Duplicates, or a distinct-list formula.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.