VBA macros · Formatting
VBA macro to unmerge cells and fill down the values in Excel
Unmerge every merged cell in the selection and fill the value into all cells of the former merged area, so the data can be sorted, filtered and pivoted.
Merged cells look tidy in a printed report but make the data almost useless: you cannot sort, filter or build a pivot table, and after unmerging only the top cell keeps the value while the others are empty. This macro unmerges every merged area in the selection and copies the value into each cell that was part of it.
Option Explicit
Sub UnmergeAndFillDown()
Dim c As Range
Dim mergedArea As Range
Dim v As Variant
Dim n As Long
If TypeName(Selection) <> "Range" Then Exit Sub
If ActiveSheet.ProtectContents Then
MsgBox "Unprotect the sheet first.", vbExclamation
Exit Sub
End If
Application.ScreenUpdating = False
For Each c In Selection.Cells
If c.MergeCells Then
Set mergedArea = c.MergeArea
v = mergedArea.Cells(1, 1).Value ' the value lives in the top-left cell
mergedArea.UnMerge
mergedArea.Value = v ' write it into every cell
n = n + 1
End If
Next c
Application.ScreenUpdating = True
MsgBox n & " merged area(s) unmerged and filled.", vbInformation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickUnmergeAndFillDownand click Run. - Select the area that contains the merged cells (or the whole sheet) before you run it.
- Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
The loop visits every cell in the selection and asks c.MergeCells, which is True for any cell that belongs to a merged area. c.MergeArea returns the whole merged block, for example A2:A6.
In a merged block, only the top-left cell holds the value; the other cells are empty. The macro reads that value first, then calls UnMerge, and finally writes the value into the entire former block with mergedArea.Value = v. Assigning one value to a multi-cell range fills every cell, so this works for blocks that span several rows, several columns or both.
After unmerging, the other cells of that block are no longer merged, so when the loop reaches them MergeCells is False and they are skipped. Each block is therefore handled once, and the counter n counts blocks, not cells.
Change it
Often the data has already been unmerged, leaving gaps under each value. This variation fills every blank cell in the selection with the value from the cell above, the classic fill down blanks trick:
Sub FillBlanksFromAbove()
Dim blanks As Range
If TypeName(Selection) <> "Range" Then Exit Sub
On Error Resume Next
Set blanks = Selection.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If blanks Is Nothing Then Exit Sub
blanks.FormulaR1C1 = "=R[-1]C" ' each blank points to the cell above
Selection.Value2 = Selection.Value2 ' turn those formulas into values
End SubR[-1]C means one row up, same column, so every blank refers to the cell above it, and because those cells are filled in order from top to bottom the value ripples down through a run of blanks. The last line freezes the result. Select more than one cell and make sure the first row of the selection is not blank. If your selection contains other formulas, they become values too. If the merged blocks held formulas and you want to keep them, copy the top-left cell's formula with mergedArea.Formula = mergedArea.Cells(1, 1).Formula instead of the value; note that relative references are not adjusted that way.
What can go wrong
Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data. Unmerging also removes the centred look of headings. Use Center Across Selection (Ctrl+1 › Alignment › Horizontal) if you want the look without the merge.If the selection contains only part of a merged block, the whole block is still unmerged and filled, because MergeArea always returns the full block. On a protected sheet, unmerging is not allowed, which is why the macro checks protection first. Very large selections such as entire columns work, but looping over a million cells takes a few seconds; select the used area instead.
Related: VBA macro to convert formulas to values in Excel, VBA macro to delete blank rows in Excel.