Re: Merging Cells with common string Values using VBA
Try this for your 3 columns:-
NB:- This Code will alter the formatting of those columns !!!!
Code
Sub Merge2()
Dim Rng As Range, Dn As Range, n As Integer, R As Range
Dim k As Variant, nRng As Range, rSt As String
Dim Dic As Object, col As Integer
For n = 0 To 2
If rSt = vbNullString Then
Set Rng = Range(Range("A1"), Range("a" & Rows.Count).End(xlUp))
Else
Set Rng = Range(Mid(rSt, 2))
End If
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
For Each Dn In Rng.Areas
For Each R In Dn
col = IIf(n = 0, 0, 1)
If Not Dic.exists(Dn.Address & R.Offset(, col).Value) Then
Dic.Add Dn.Address & R.Offset(, col).Value, R.Offset(, col)
Else
Set Dic.Item(Dn.Address & R.Offset(, col).Value) = Union(Dic.Item(Dn.Address & R.Offset(, col).Value), R.Offset(, col))
End If
Next R
Next Dn
rSt = vbNullString
Application.DisplayAlerts = False
For Each k In Dic.keys
If Dic.Item(k).Count > 1 Then
With Dic.Item(k)
rSt = rSt & "," & .Address
.MergeCells = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End If
Next k
Application.DisplayAlerts = True
Next n
End Sub
Display More