Excel VBA Macro to Merge Blank Cells with a Populated Cell
Question details
Create an Excel VBA macro that merges blank cells with the appropriate populated cell in each section.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet contains sections that may have different numbers of rows and may be outlined by borders.
- Observed behavior
- The desired result is one merged cell per section when that section contains exactly one populated cell.
Check whether each section occupies one column, has a closed border, and contains exactly one populated cell. Decide what should happen when a section contains multiple values or formulas; the example below skips those cases.
Merge One Selected Section with an Excel VBA Macro
Use this example when each section is a single-column range with one constant value and you can select each section manually.
This macro does not detect borders automatically. It merges the selected range only, so select the cells inside one section at a time. It skips selections with formulas, existing merged cells, or anything other than one populated cell.
The macro moves the value to the top cell before merging. Run it only on ranges outside Excel tables, and check that the worksheet is not protected. VBA actions generally cannot be undone with Excel's Undo command.
In Excel, open the Developer tab and select Visual Basic. In the VBA editor, choose Insert > Module.
Paste this code into the module: Sub MergeSelectedSection() Dim r As Range, c As Range, sourceCell As Range If TypeName(Selection) <> "Range" Then Exit Sub Set r = Selection If r.Areas.Count <> 1 Then MsgBox "Select one continuous section." Exit Sub End If If r.Columns.Count <> 1 Or r.Rows.Count < 2 Then MsgBox "Select at least two cells in one column." Exit Sub End If If r.Worksheet.ProtectContents Then MsgBox "Unprotect the worksheet first." Exit Sub End If For Each c In r.Cells If c.MergeCells Or c.HasFormula Then MsgBox "The selection contains merged cells or formulas." Exit Sub End If If Application.WorksheetFunction.CountA(c) > 0 Then If Not sourceCell Is Nothing Then MsgBox "The section contains more than one populated cell." Exit Sub End If Set sourceCell = c End If Next c If sourceCell Is Nothing Then MsgBox "The section has no populated cell." Exit Sub End If If sourceCell.Address <> r.Cells(1, 1).Address Then r.Cells(1, 1).Value2 = sourceCell.Value2 sourceCell.ClearContents End If r.Merge End Sub
Return to the worksheet and select the cells inside one single-column section. Choose Developer > Macros, select MergeSelectedSection, and click Run.
Check the merged cell and its displayed value, then select the next section and run the macro again. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro.

Work with Excel Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and work with their contents while moving between office suites.

Frequently Asked Questions
Will the macro find bordered sections automatically?
No. Select the cells inside one section before running MergeSelectedSection from Developer > Macros. Automatic detection requires additional rules for interpreting borders.
What happens if a section contains two populated cells?
The example displays a message and exits without merging. Define a separate rule before changing the macro to handle multiple values.
Can the selected sections have different numbers of rows?
Yes. Each selection can have a different height, provided it is one continuous column with at least two cells and exactly one populated cell.
Why does the macro skip cells containing formulas?
Moving a formula can change its references or behavior. The example stops when it finds a formula; decide how formulas should be handled before adapting the code.
How do I keep the macro in my workbook?
In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm). Follow your organization's macro security settings when reopening it.




