• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Merge similar data in Column A

YasserKhalil

Well-Known Member
Hello everybody
I have some values in range("A3:A100"). There are similar data sometime in the range
I need to merge similar and adjacent cells.
Say I have in A3:A6 the value ("A") I want to merge these cells to be one cell.
And vice versa to unmerge the range so as to be as it was
 
I found solution
Code:
Sub MergeSameCell()
    Dim Rng As Range, xCell As Range
    Dim xRows As Integer
    Set WorkRng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
        xRows = WorkRng.Rows.Count
        For Each Rng In WorkRng.Columns
            For i = 1 To xRows - 1
                For j = i + 1 To xRows
                    If Rng.Cells(i, 1).Value <> Rng.Cells(j, 1).Value Then
                    Exit For
                    End If
                Next
                WorkRng.Parent.Range(Rng.Cells(i, 1), Rng.Cells(j - 1, 1)).Merge
                i = j - 1
            Next
        Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Sub UnMergeSameCell()
    Dim Rng As Range, xCell As Range
    Set WorkRng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
        For Each Rng In WorkRng
            If Rng.MergeCells Then
                With Rng.MergeArea
                    .UnMerge
                    .Formula = Rng.Formula
                End With
            End If
        Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Back
Top