Excel VBA Merge Range: How to Merge Cells Safely with VBA

VBA & macros · Updated November 21, 2026

To merge a range with Excel VBA, call the Merge method on a Range object, for example Range("B2:D2").Merge. Excel keeps the value or formula in the upper-left cell and removes the contents of the other cells, so check the range before running a macro.

What merging a range means

Merging combines two or more adjacent cells into one larger cell for display and editing. The merged cell takes the address of the range’s upper-left cell. For example, merging B2:D2 creates one visible cell that is addressed as B2. This is often used for a report title or a heading spanning several columns.

A merge changes the worksheet layout; it does not combine the contents of the cells. If B2 contains “Quarterly Report” and C2 contains “Draft,” merging B2:C2 leaves “Quarterly Report” and discards “Draft.” The same applies if the cells contain formulas: only the upper-left cell’s formula remains. This behavior is a common source of accidental data loss.

Merged cells also behave differently from ordinary cells in tasks such as sorting, filtering, copying, and selecting ranges. For that reason, merging is usually best reserved for presentation areas, rather than tables that people need to sort, filter, or analyze.

Basic VBA syntax

The basic method is Range.Merge. You can use a worksheet-qualified range to make clear which sheet the macro should change. This example merges a heading area and centers its text:

Sub MergeReportHeading()
    Dim headingRange As Range

    ' Refer to a specific worksheet and range.
    Set headingRange = ThisWorkbook.Worksheets("Summary").Range("B2:F2")
    headingRange.Merge
    headingRange.HorizontalAlignment = xlCenter
    headingRange.VerticalAlignment = xlCenter
End Sub

The worksheet name in the example must match a sheet in the workbook containing the macro. If the name is misspelled or the sheet does not exist, VBA raises an error. Qualifying the range with a worksheet is safer than relying on whichever sheet happens to be active when the macro runs.

For a quick one-off command, a single line also works: Worksheets("Summary").Range("B2:F2").Merge. In a longer macro, assigning the range to a named variable makes it easier to reuse the range and set its alignment, wrapping, or other formatting afterward.

Merge a selected range more safely

A macro that merges the current selection can be convenient, but it should check that the selection is a single rectangular range and that it does not contain multiple populated cells. The following example stops rather than silently discarding data. It counts formulas as content, even if a formula currently displays a blank result.

Sub MergeSelectionSafely()
    Dim target As Range

    ' Confirm that a cell range, not a chart or shape, is selected.
    If TypeName(Selection) <> "Range" Then
        MsgBox "Select a cell range first."
        Exit Sub
    End If

    Set target = Selection

    ' Do not merge separate, noncontiguous selection areas.
    If target.Areas.Count <> 1 Then
        MsgBox "Select one continuous rectangular range."
        Exit Sub
    End If

    If target.Cells.CountLarge < 2 Then
        MsgBox "Select at least two cells."
        Exit Sub
    End If

    ' More than one nonempty cell means merging would discard content.
    If Application.CountA(target) > 1 Then
        MsgBox "More than one cell contains data. Review the range first."
        Exit Sub
    End If

    target.Merge
    target.HorizontalAlignment = xlCenter
    target.VerticalAlignment = xlCenter
End Sub

The macro checks the selection before changing it, but it does not decide which value should win when several cells contain information. Review those cells yourself, or move and combine their contents using a separate procedure before merging. To run any macro, save a copy of the workbook first: VBA changes cannot be reversed with Ctrl+Z.

Choose the right kind of merge

By default, Merge turns the entire specified range into one cell. A range covering B2:D4 therefore becomes one merged block. If you want a separate merged cell on each row, use the optional Across argument:

Sub MergeEachRow()
    Dim rowRange As Range

    ' Each row in B2:D4 becomes a separate merged cell.
    Set rowRange = ThisWorkbook.Worksheets("Summary").Range("B2:D4")
    rowRange.Merge Across:=True
End Sub

With this example, B2:D2, B3:D3, and B4:D4 are merged separately. This is different from merging all nine cells into one block. Use Across:=True only when each row should remain its own visual unit. The same warning about keeping only the upper-left cell’s content applies to each row-level merge.

To check whether a range is merged, use its MergeCells property. To find the full merged area containing a particular cell, use MergeArea on a single-cell range. For example, Worksheets("Summary").Range("C2").MergeArea.Address returns the address of the merged block containing C2, if that cell belongs to one. These properties help a macro inspect layout before formatting or unmerging cells.

What happens to data and formatting

Before merging, decide what should happen to every nonempty cell in the proposed range. Excel does not join text with a separator or preserve multiple formulas in the merged cell. It keeps the upper-left entry, so a merge can erase information without creating a combined replacement. A confirmation prompt may appear when merging through Excel’s interface; do not treat that prompt as a substitute for checking the data.

Formatting is separate from cell content. After a merge, you can set horizontal and vertical alignment, turn on text wrapping, or adjust the row height. For example, target.WrapText = True enables wrapping for a range assigned to the variable target. A long heading may still require a taller row to display all its lines.

References to merged cells can also surprise readers of a workbook. The merged area is represented by its upper-left cell for formulas and many VBA operations. A formula referring to B2 can read the value in the merged range B2:D2, while a reference to C2 does not point to a separate visible cell with its own value. Keep formulas and data-entry logic anchored to the upper-left cell.

Unmerge cells with VBA

Use the UnMerge method to split merged cells back into individual cells. For example, this procedure unmerges a specific area on the Summary sheet:

Sub UnmergeReportHeading()
    Dim headingRange As Range

    ' Identify the area to restore as separate cells.
    Set headingRange = ThisWorkbook.Worksheets("Summary").Range("B2:F2")
    headingRange.UnMerge
End Sub

Unmerging restores the cell grid, but it does not recover values that were discarded when the cells were originally merged. The surviving entry remains in the upper-left cell, and the other cells are blank unless you fill them. If you are unsure which cells are merged, inspect the worksheet first; a range can contain more than one merged area.

Run the macro in Excel for Microsoft 365

These instructions are for the desktop version of Excel for Microsoft 365 on Windows. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain its VBA code. In the ribbon, choose Developer > Visual Basic, then use Insert > Module in the Visual Basic Editor and paste the procedure into the module.

  1. Return to Excel and select the cells if the macro works on the current selection.
  2. Press Alt+F8, choose the macro name, and select Run. You can also run a procedure from the Visual Basic Editor.
  3. Check the result, including the retained value, alignment, and any effects on nearby layout.

If the Developer tab is not visible in Windows Excel, enable it through File > Options > Customize Ribbon by checking Developer. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros; use the desktop app for those tasks. Excel for Mac supports VBA in the desktop app, although keyboard shortcuts can vary with the keyboard and macOS settings. Use the Developer tab or Excel’s macro tools if a Windows shortcut does not work.

Common problems and practical limits

  • “We can’t do that to a merged cell” or an unexpected result: The target may overlap a previously merged area. Check the worksheet’s existing merges and make the target range consistent before applying another merge.
  • Data disappears: More than one cell contained a value or formula. The merge keeps the upper-left entry; restore from a saved copy if the removed content matters.
  • Sorting or filtering becomes awkward: Merged cells can make records uneven and interfere with table-like operations. Avoid merging inside data lists that need sorting or filtering.
  • Text appears cut off: Set wrapping or adjust row height and column width after merging. A merged range does not guarantee that a long heading will fit on screen.
  • The macro changes the wrong sheet: An unqualified Range can refer to the active sheet. Refer to the intended worksheet explicitly, as in the examples.

Excel tables have their own structured layout and do not allow cells within the table body to be merged in the normal way. Keep table data unmerged; place a decorative heading outside the table if one is needed. Merging may also complicate accessibility and navigation because the visible cell layout no longer matches the underlying grid.

Alternatives to merging cells

If the goal is to center a heading over several columns without combining the cells, consider Center Across Selection. Select the cells, open Home > Alignment using the dialog launcher, then set Horizontal to Center Across Selection. The cells remain separate, preserving a more regular grid for selection and worksheet operations. This option is often a better fit for a heading above data.

Another option is to put the heading in one cell and adjust the column widths, or use a text box for a purely decorative label. These approaches avoid some of the layout constraints created by merged cells. Choose a real merge when the larger cell is part of the intended form design; choose an alternative when the cells need to remain individually usable.

Frequently Asked Questions

What is the VBA code to merge cells A1 to C1?
Use Range("A1:C1").Merge on the intended worksheet. For clarity in a macro, qualify it as Worksheets("Sheet1").Range("A1:C1").Merge, replacing Sheet1 with the actual sheet name.

Does VBA merge keep all the values?
No. A standard merge keeps the upper-left cell’s value or formula and removes the contents of the other cells. Check the range first if any of those entries matter.

How do I merge each row of a range separately?
Use Range("B2:D4").Merge Across:=True. This makes one merge per row in the specified range, rather than merging all of it into one cell.

How do I unmerge a range in VBA?
Call UnMerge on the range, such as Worksheets("Summary").Range("B2:F2").UnMerge. Unmerging restores separate cells but does not bring back values removed by the earlier merge.

Can I run Excel VBA in a browser?
No. Excel for the web does not run VBA macros. Open the workbook in the Excel desktop app for Windows or Mac to use VBA.

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.