I'm trying to create a simple sub-routine which will merge multiple rows if cells in column A contain the same numerical data.
Option Explicit
Sub MergeRows()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim lastRow As Long
Dim i As Long
lastRow = ThisWorkbook.Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
If ThisWorkbook.Worksheets(1).Cells(i, 1).Value = _
ThisWorkbook.Worksheets(1).Cells(i + 1, 1).Value Then
ThisWorkbook.Worksheets(1).Range(ThisWorkbook.Worksheets(1) _
.Cells(i, 1), ThisWorkbook.Worksheets(1).Cells(i + 1, 1)).Merge
Else
End If
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
The code above only merges 2 lines at a time, so if there were three lines with the same value, it would merge two rows, then leave the remainder in a row to itself.
For i = lastRow To 1 Step -1