How to merge cells based on similar values - Excel 2010

爷,独闯天下 提交于 2019-12-01 00:19:40
Option Explicit

Private Sub MergeCells()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim rngMerge As Range, cell As Range
    Set rngMerge = Range("A1:A100") 'Set the range limits here

MergeAgain:
    For Each cell In rngMerge
        If cell.Value = cell.Offset(1, 0).Value And IsEmpty(cell) = False Then
            Range(cell, cell.Offset(1, 0)).Merge
            GoTo MergeAgain
        End If
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

You can hardcode the range limits (i.e. the last row in Column A to check), have the user input it each time, or programmatically find the last row in the range . Either way, this should get you started.

By the way, you could find the last row in column A with the following:

Dim i As Integer
    i = Range("A1").End(xlDown).Row
    Set rngMerge = Range("A1:A" & i)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!