Merge Excel Columns A–C by Matching Groups with VBA
Question details
Merge cells in columns A, B, and C for contiguous matching groups in column A without deleting worksheet rows.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet has more than 7,000 rows, including unique values in other columns that must remain in place.
- Observed behavior
- Matching groups should be merged across columns A–C while all worksheet rows and values in other columns remain intact.
Save a test copy as an Excel Macro-Enabled Workbook (.xlsm), and check that the target cells in columns A–C are not already merged. Merging keeps only the upper-left cell's value, so review values and formulas in each group before running the macro.
Run a VBA macro to merge contiguous groups in columns A–C
Use this macro when column A contains consecutive matching values and every worksheet row must remain.
The macro groups consecutive, nonblank values in column A using case-sensitive text comparisons. It merges each group separately in columns A, B, and C; it does not delete rows or change other columns.
Before merging, it checks the entire selection. It stops if a lower cell contains a formula or if a lower value in B or C differs from the group's top value. Blank and duplicate lower values are removed by merging.
In the test workbook, open Developer > Visual Basic, or press Alt+F11 in Excel.
Choose Insert > Module and paste this code: Sub MergeMatchingGroups() Dim ws As Worksheet, sel As Range, cell As Range Dim firstRow As Long, lastRow As Long, a As Long, b As Long Dim r As Long, c As Long, phase As Long Dim oldAlerts As Boolean, oldScreen As Boolean oldAlerts = Application.DisplayAlerts oldScreen = Application.ScreenUpdating On Error GoTo Failed If TypeName(Selection) <> "Range" Then Err.Raise vbObjectError + 1, , "Select cells in column A." Set sel = Selection If sel.Areas.Count <> 1 Or sel.Column <> 1 Or sel.Columns.Count <> 1 Then Err.Raise vbObjectError + 1, , "Select one contiguous range in column A." End If Set ws = sel.Worksheet firstRow = sel.Row lastRow = firstRow + sel.Rows.Count - 1 For Each cell In ws.Range(ws.Cells(firstRow, 1), ws.Cells(lastRow, 3)) If cell.MergeCells Then Err.Raise vbObjectError + 1, , "Unmerge existing merged cells in columns A–C first." If IsError(cell.Value2) Then Err.Raise vbObjectError + 1, , "Resolve error values in columns A–C first." Next cell Application.ScreenUpdating = False For phase = 1 To 2 a = firstRow Do While a <= lastRow b = a If Len(CStr(ws.Cells(a, 1).Value2)) > 0 Then Do While b < lastRow If CStr(ws.Cells(b + 1, 1).Value2) <> CStr(ws.Cells(a, 1).Value2) Then Exit Do b = b + 1 Loop End If If b > a Then For c = 1 To 3 If phase = 1 Then For r = a + 1 To b If ws.Cells(r, c).HasFormula Then Err.Raise vbObjectError + 1, , "A lower cell in a group contains a formula." If c > 1 Then If Len(CStr(ws.Cells(r, c).Value2)) > 0 Then If CStr(ws.Cells(r, c).Value2) <> CStr(ws.Cells(a, c).Value2) Then Err.Raise vbObjectError + 1, , "A group contains different values in column B or C." End If End If Next r Else Application.DisplayAlerts = False ws.Range(ws.Cells(a, c), ws.Cells(b, c)).Merge End If Next c End If a = b + 1 Loop Next phase Application.DisplayAlerts = oldAlerts Application.ScreenUpdating = oldScreen MsgBox "Matching groups merged; no rows were deleted." Exit Sub Failed: Application.DisplayAlerts = oldAlerts Application.ScreenUpdating = oldScreen MsgBox "Macro stopped: " & Err.Description End Sub
Return to the worksheet and select one contiguous range in column A, excluding the header. Include all rows of each group you want to merge.
Open Developer > Macros, select MergeMatchingGroups, and click Run. If the macro reports a conflicting value or formula, inspect the selected rows before trying again.
Review columns A–C and the other columns in the test copy. Confirm that each intended group is merged and that all rows remain.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move between office suites. You can merge selected cells manually in WPS Spreadsheets; Excel VBA automation and macro compatibility depend on your WPS version and configuration.

Frequently Asked Questions
Will merging cells delete the worksheet rows?
No. The macro merges cells within columns A–C without deleting rows, so values in other columns stay on their original rows.
What happens if column B or C has different values in a group?
The macro stops before merging if a nonblank lower value in B or C differs from the group's top value. Review those cells before deciding how to handle them.
Does the macro merge matching values that are separated by other rows?
No. It merges only consecutive matching values in column A within the selected range. Blank cells in column A are not grouped.
Why should I save the workbook as an XLSM file?
An Excel Macro-Enabled Workbook preserves VBA code. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm) before adding or saving the macro.




