Excel Macro to Merge Cells: VBA Steps and Limits

VBA & macros · Updated November 19, 2026

Yes. In Excel for Microsoft 365 on Windows, a VBA macro can merge the currently selected cells with the Range.Merge method. The key caution is that Excel keeps only the value in the range’s upper-left cell; check the selection and preserve any other contents before running the macro.

What a cell-merging macro does

When you merge a rectangular range, Excel turns it into one larger cell. For example, merging A1:C1 creates one cell spanning three columns. The merged cell is still addressed through its upper-left cell, A1, and that is where its value is stored. A merge changes the layout; it does not combine the text or numbers from every cell into a new value.

In VBA, the essential instruction is Range("A1:C1").Merge. A macro can apply that instruction to a fixed address, to the current selection, or to a series of ranges. The code below works on the current selection, which makes it reusable for different tables. It checks that the selection is a single, multi-cell range and asks before merging.

Excel’s ribbon offers a no-code version of the same general task: select cells, then choose Home > Merge & Center or open its dropdown for other merge options. A macro is useful when you repeat a consistent operation, but it does not make merging safer by itself. The same layout and data-loss limitations apply.

VBA macro for the selected range

In the code, target refers to the selected range. The confirmation is intentional: values outside the upper-left cell will not be retained as cell contents after merging. If you need those values, cancel and combine or copy them somewhere first.

Sub MergeSelectedCells()
    Dim target As Range
    Dim answer As VbMsgBoxResult

    ' Make sure the current selection is a cell range.
    If TypeName(Selection) <> "Range" Then
        MsgBox "Select a cell range first."
        Exit Sub
    End If

    Set target = Selection

    ' A single merge cannot operate on separate selected areas.
    If target.Areas.Count > 1 Then
        MsgBox "Select one rectangular range, not multiple areas."
        Exit Sub
    End If

    If target.Cells.CountLarge = 1 Then
        MsgBox "Select at least two cells to merge."
        Exit Sub
    End If

    answer = MsgBox( _
        "Merge the selected cells? Only the upper-left value will remain.", _
        vbYesNo + vbExclamation, _
        "Confirm cell merge")

    If answer <> vbYes Then Exit Sub

    target.Merge
End Sub

This macro merges a single rectangular selection into one cell. It does not join text, choose a merge pattern for each row, or automatically restore the original layout. A protected worksheet or another workbook condition that prevents editing can also stop the operation. Excel may display its own warning in some situations, but the macro’s confirmation gives the user an explicit chance to reconsider.

How to add and run the macro

  1. Save a copy of the workbook before changing its structure. Macros cannot be undone with Ctrl+Z, so an ordinary undo history is not a recovery plan.
  2. Save the working file as an Excel Macro-Enabled Workbook (.xlsm) if you want the VBA code to remain in the file.
  3. Open the VBA editor from Developer > Visual Basic. If the Developer tab is not visible in Windows Excel, enable it under File > Options > Customize Ribbon.
  4. In the editor, choose Insert > Module, paste the code into the module, and return to the worksheet.
  5. Select one rectangular range, then run MergeSelectedCells from Developer > Macros. Read the confirmation before choosing Yes.

Excel for Mac supports VBA, although the surrounding controls and some Windows-specific VBA features differ. This example uses basic Excel range operations rather than Windows APIs. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros; use the desktop application for this procedure. Do not enable macros indiscriminately: only run code from a source and workbook you trust.

What happens to values and formatting

Consider A1:C1, where A1 contains “North,” B1 contains “South,” and C1 contains “West.” After merging, the merged cell retains “North”; the other two cell values are discarded as separate contents. Merging is therefore not a substitute for concatenation. If the goal is to produce “North, South, West” in one cell, combine the text first, for example with a formula or a separate text-joining process, and verify the result before merging.

Only the upper-left cell’s value is retained, and that cell becomes the reference point for the merged area. Formatting can also be affected by the operation: do not assume that every cell’s individual formatting will survive as an independent format after those cells become one. If appearance matters, check the result and apply the desired number format, fill, border, alignment, and wrapping to the merged cell.

Merging can also make a sheet harder to work with later. Sorting and filtering a table, selecting individual cells, pasting rectangular data, and adjusting row heights or column widths can become less straightforward when the affected area contains merged cells. This is why merges are usually best reserved for presentation elements, such as a report heading, rather than the body of a data table.

Choosing the right merge option

The standard target.Merge instruction creates one merged cell for the selected rectangle. Excel also offers options that look similar but have different effects. Choose based on the layout you need, not just on how the cells look at first glance.

  • Merge & Center: merges the selected cells and centers the remaining content. It is a ribbon option; the basic VBA Merge method does not itself mean “center.” Set alignment separately if needed.
  • Merge Across: merges cells separately across each row in the selection. For example, Range("B2:D4").Merge Across:=True creates three merged cells: B2:D2, B3:D3, and B4:D4. Each row retains the value in its own leftmost cell.
  • Merge Cells: merges the selection without applying the centering implied by Merge & Center.
  • Unmerge Cells: separates a merged area back into its component cells, but does not bring back values that were discarded during the original merge.

For a worksheet used as a data source, a better choice is often to leave the cells unmerged and use Home > Alignment options to position the text. In particular, Center Across Selection can create a centered heading across columns without making them one cell. That preserves the separate cells, which generally makes the range easier to sort, copy, and analyze.

Common macro problems and practical fixes

  • The macro says to select one range: the selection may contain separate areas, such as cells selected with Ctrl. Select one continuous rectangle instead. A single call to Merge is not intended to merge unrelated areas as one block.
  • Some contents disappear: this is the expected consequence of merging cells that contain multiple values. Cancel when prompted, copy the contents elsewhere, or combine them before merging.
  • The merge is blocked: check whether the worksheet is protected or whether the selected range is otherwise unavailable for editing. Resolve the workbook restriction only if you are authorized to do so.
  • The result is not centered: Range.Merge merges cells but does not guarantee centered alignment. Set horizontal alignment separately, or use the ribbon’s Merge & Center command when that is the intended presentation.
  • The workbook will not retain the code: save in .xlsm format. A macro-enabled file can still be blocked by security settings, so follow your organization’s policy rather than lowering macro security broadly.

If you are unsure what the selection contains, inspect it before running the macro. In particular, check for formulas and values in cells other than the top-left one. A workbook copy is useful not only for recovering data but also for comparing the original table with the merged result.

When merging is appropriate—and when it is not

Merging is most defensible in a small, stable presentation area: a report title spanning columns, a form label covering a fixed block, or a printed layout where the merged shape is part of the design. In those cases, the range is not expected to behave like a sortable list, and the upper-left cell is the intended location for the displayed content.

A typical example is a monthly report with a heading in A1 and a table beginning on row 3. Selecting A1:D1 and running the macro makes a single heading area above the table. By contrast, merging A3:A20 in a table of records can interfere with sorting and make it unclear which record each row belongs to. Keep each record’s data in its own row and avoid merged cells inside the dataset whenever possible.

If a workbook will feed charts, PivotTables, Power Query, or another system, consistent rows and columns are usually more valuable than a visually continuous heading. A simple, unmerged structure is easier to filter, validate, export, and reuse. You can still make a heading prominent with cell styles, borders, fill colors, or centered alignment across a selection.

Frequently Asked Questions

Can an Excel macro merge cells without losing data?
No—not by merging alone. Excel retains the upper-left cell’s value and does not preserve the other values as separate contents. Combine or copy those values before merging if they matter.

What is the VBA command to merge cells?
Use the Merge method on a range, such as Range("A1:C1").Merge. For a reusable macro that acts on selected cells, set a Range variable to Selection and call target.Merge after validating the selection.

Can I merge each row separately with a macro?
Yes. VBA’s Merge Across:=True option merges each row of the specified range separately. For example, B2:D4 becomes three row-level merges, not one large merged block.

Can I undo a cell merge macro with Ctrl+Z?
No. Running VBA clears Excel’s normal undo history, so Ctrl+Z cannot reverse the macro. Save a copy first; if you only need to reverse the layout, Unmerge Cells can separate the area, but it cannot recover discarded values.

Can Excel for the web run a VBA merge macro?
No. Excel for the web does not run, create, or edit VBA macros. Open the workbook in the desktop Excel application to use VBA.

Should I merge cells in an Excel table?
Usually not in the data area. Merged cells can complicate sorting, filtering, and other operations on structured records. Keep table rows and columns consistent, and use alignment or formatting for headings instead.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

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

Spotted an error in this guide? Let us know.