You merge cells in Excel VBA with the Merge method on a Range object, for example Range("A1:C1").Merge. That single line combines the selected cells into one and keeps the value from the top-left cell, discarding anything in the others. You can also use Merge Across:=True to merge each row of a multi-row range separately, or MergeCells = True to apply the setting to any range object. This guide covers the working code, how to place it in the right module, and how to avoid the errors that make merging macros fail.

What You'll Need

  • A desktop copy of Excel for Windows or Mac with macros enabled. The Visual Basic Editor is not available in Excel for the web or the mobile apps, so you cannot write or run VBA there.
  • A workbook saved in a macro-enabled format (.xlsm) if you want the code to persist after closing. Plain .xlsx files drop macros on save.
  • Macro security set to allow your code to run. The options are under File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows, and Excel > Settings (or Preferences) > Security on Mac; menu labels shift slightly between versions.
  • Basic familiarity with opening the editor: Alt+F11 on Windows, or Option+F11 on Mac.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in its own window with a Project pane listing your open workbooks.
  2. Insert a standard module. In the editor, choose Insert > Module. A blank code window appears. Standard modules are the right home for general-purpose routines that you will run manually or call from buttons; do not put them in a sheet module unless the code must react to an event on that sheet.
  3. Write the basic merge routine. Paste the following into the module: Sub MergeHeader() Range("A1:D1").Merge End Sub This merges A1 through D1 into one cell. The text in A1 survives; values in B1 to D1 are lost unless you handle them first.
  4. Point it at the right worksheet. If the active sheet is not the one you mean, qualify the range: Worksheets("Report").Range("A1:D1").Merge. Qualifying avoids merging cells on whatever sheet happens to be visible.
  5. Merge each row separately when needed. For a block such as A1:D10 where every row should become its own merged cell, use Range("A1:D10").Merge Across:=True. Without Across, Excel merges the whole block into a single cell, which is usually not what you want.
  6. Optionally merge the selection instead. To let the user highlight cells and run the macro, use Selection.Merge. This works on the current selection and is handy behind a button.
  7. Run the macro. Press F5 inside the editor while the cursor is in the routine, or go back to Excel and use Developer > Macros (Alt+F8), select the macro name and click Run. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
  8. Confirm the result on the sheet. The merged cell spans the range you specified and shows the top-left value. Check that no important data disappeared in the merge.
  9. Decide whether to unmerge later. The reverse command is Range("A1:D1").UnMerge. It restores individual cells but cannot bring back values that were discarded when you merged.
  10. Save the workbook. Use File > Save As, choose Excel Macro-Enabled Workbook (.xlsm), and keep it outside any folder your organisation blocks for macros.

If you want to avoid the warning dialog that appears when a merge would delete non-empty cells, set Application.DisplayAlerts = False before the merge and restore it with True immediately afterwards. Only do this after you have verified the code, because it suppresses the confirmation that would otherwise tell you data is about to be lost.

Common Mistakes

Losing data silently. Merging keeps only the upper-left value. Loop through the cells and concatenate the text first if every entry matters, for example joining the contents with a separator before you merge. Merging the block instead of each row. Forgetting Merge Across:=True turns a grid into one giant cell. Leaving DisplayAlerts off. If the macro errors midway, Excel stays silent for the rest of the session, hiding other warnings. Reset it in an error handler. Assuming merged cells behave like normal cells. Sorting, filtering, copy-paste and formulas that reference merged ranges behave differently, and macros that loop over every cell can trip over merged areas. Putting the code in the wrong place. A routine in a sheet module that references an unqualified Range acts on that sheet only, which can confuse you when you switch sheets. Running under the wrong selection. Selection.Merge fires on whatever happens to be highlighted, which may be a single cell, producing an error or no visible change.

Tips

  • Test merges on a copy of the workbook. Merging is destructive to adjacent values and cannot be undone by the macro itself, only by pressing Ctrl+Z before saving.
  • Store the target range in a variable and set Application.ScreenUpdating = False at the start and True at the end if you are merging many ranges. This speeds up loops noticeably.
  • Use MergeCells = True when you want to set the property rather than call a method; both work, and the property form reads more clearly in some code.
  • Add a small guard before merging: check that the range has more than one cell with If rng.Cells.Count > 1 Then, so a single-cell range does not cause an error.
  • Consider centring the result with HorizontalAlignment = xlCenter for header rows; merged headers look odd when left-aligned.
  • Keep a backup of macro-enabled workbooks. Excel blocks files from untrusted locations, so if a macro stops running, check the Trust Center before rewriting the code.

Troubleshooting

The macro does nothing. Macros may be disabled by the Trust Center or the file may be saved as .xlsx instead of .xlsm. Check the macro settings and re-save in the macro-enabled format.

Run-time error 1004. This usually means the range reference is invalid, the sheet name is misspelled, or the workbook is not the active one. Check the sheet name spelling and that the workbook is open and referenced correctly.

Run-time error 5 or a general argument error. Often caused by an unqualified or empty Selection. Make sure cells are actually highlighted before running a selection-based macro.

Data disappears after the merge. That is expected behaviour, not a bug. The other values were discarded. Restore from a backup or undo immediately with Ctrl+Z before saving.

Excel warns about losing data every time. That is the built-in confirmation dialog seeing non-empty cells in the range. Either clear those cells first or suppress alerts deliberately as described above.

The code cannot be found in the macro list. Confirm the routine is Public Sub in a standard module, not a private routine in a sheet or class module.

Frequently Asked Questions

Can I merge cells only when they contain the same value?

Yes. Loop through the range and check each cell against the previous one, merging only the matching runs. This takes a few more lines but avoids wiping distinct data.

Does merging with VBA keep all the text from the cells?

No. Only the top-left value is kept. Concatenate text into one cell first if you need every entry preserved.

What is the difference between Merge and Merge Across?

Merge combines the entire range into one cell. Merge Across:=True merges each row of the range separately, so a 10-row block becomes 10 merged cells.

Can I undo a merge done by a macro?

Only with Excel's own undo (Ctrl+Z) before saving, or by running UnMerge. UnMerge restores the cells but not any values that were discarded.

Why does VBA not work in Excel for the web?

Excel for the web and the mobile apps do not include the Visual Basic Editor. You need the desktop application to create or edit macros.