Sub Tach_file()
Dim lastRow As Long, i As Long, r As Long, c As Long, data(), item(), result(), Xom As Variant
Dim shp As Shape, dic As Object, sh As Worksheet
Set sh = ActiveSheet
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
lastRow = sh.Cells(Rows.Count, "N").End(xlUp).Row
If lastRow < 5 Then Exit Sub
data = sh.Range("A5:AY" & lastRow).Value 'Lay toi dong cuoi cung chua xom
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
For i = 1 To UBound(data)
Xom = Trim(data(i, 14))
If Len(Xom) Then
If Not dic.exists(Xom) Then
ReDim item(1 To 1)
item(1) = i
dic.Add Xom, item
Else
item = dic.item(Xom)
ReDim Preserve item(1 To UBound(item) + 1)
item(UBound(item)) = i
dic.item(Xom) = item
End If
End If
Next
If dic.Count Then
For Each Xom In dic.keys
item = dic.item(Xom)
ReDim result(1 To UBound(item), 1 To UBound(data, 2))
For r = 1 To UBound(item)
For c = 1 To UBound(data, 2)
result(r, c) = data(item(r), c)
Next c
Next r
sh.Copy
With ActiveSheet
.Range("A5:AY1000").Clear
For Each shp In .Shapes
shp.Delete
Next shp
With .Range("A5").Resize(UBound(result), UBound(result, 2))
.Value = result
.Borders.LineStyle = xlContinuous
End With
.Parent.SaveAs ThisWorkbook.Path & "\" & Xom, xlOpenXMLWorkbook
.Parent.Close
End With
Next Xom
End If
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub