sub merge()
Dim LR As Long, i As Long, Arr As Range, Rng As Range
Dim k As Long, LR1 As Long
LR1 = Sheets("du lieu").Cells(Rows.Count, "L").End(xlUp).Row
LR = Sheets("du lieu").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("du lieu").Range("A2:A" & LR).UnMerge
Set Arr = Sheets("du lieu").Range("B2:B" & LR)
For Each Rng In Arr
With Sheets("du lieu")
For k = 2 To LR1
If Rng.Value = .Range("L" & k).Value Or Rng.Value = .Range("M" & k).Value Or Rng.Value = .Range("N" & k).Value Then
Rng.Offset(0, -1).Value = .Range("K" & k)
End If
If Rng.Value = .Range("L" & k).Value Then
Rng.Font.Color = 100
End If
If Rng.Value = .Range("M" & k).Value Then
Rng.Font.Color = 2000
End If
If Rng.Value = .Range("N" & k).Value Then
Rng.Font.Color = 30000
End If
Next k
End With
Next Rng
'---------- sort
Sheet5.Sort.SortFields.Clear
With ActiveSheet.Sort
.SortFields.Add Key:=[A2], SortOn:=xlSortOnValues, Order:=xlAscending
.SortFields.Add([B2], xlSortOnFontColor).SortOnValue.Color = 100
.SortFields.Add([B2], xlSortOnFontColor).SortOnValue.Color = 2000
.SortFields.Add([B2], xlSortOnFontColor).SortOnValue.Color = 30000
.SetRange Range("A1:B" & LR)
.Header = xlYes
.Apply
End With
Sheet5.Range("A1:B" & LR).Font.Color = 0
'-------------meger
Application.DisplayAlerts = False
For i = LR To 2 Step -1
With Sheets("du lieu")
If .Cells(i, "A") = .Cells(i - 1, "A") Then
.Range(Cells(i, "A"), Cells(i - 1, "A")).merge
End If
End With
Next i
Application.DisplayAlerts = True
Set Arr = Nothing
End Sub