Option Explicit
Sub SoSanhDuLieu()
Dim lRow As Long, cRow As Long, dRow As Long, jJ As Long
Dim jW As Long, jZ As Long: Dim iJ As Integer
Dim Rng As Range, RngC As Range: Dim sCot As String
Application.ScreenUpdating = False
lRow = [c65432].End(xlUp).Row: Range("A38:R321").Clear ''
Range("F2:O2").Copy Destination:=Range("F38")
[A38] = "Ten cot": [b38] = "So TT"
[c38] = "Dong-Cot"
Range("A38:O38").Select: Selection.Font.Bold = True
For jW = 18 To 23
cRow = Range(Chr(64 + jW) & 36).End(xlUp).Row
If cRow = 1 Then GoTo 17
For jJ = 3 To lRow
For jZ = 2 To cRow
If Cells(jJ, 3) = Cells(jZ, jW) Then
If Rng Is Nothing And RngC Is Nothing Then
sCot = Cells(jZ - 1, jW) ''
Set Rng = Cells(jZ, jW) 'Cot i'
Set RngC = Cells(jJ, 3).Resize(1, 13)
Else
Set Rng = Union(Rng, Cells(jZ, jW))
Set RngC = Union(RngC, Cells(jJ, 3).Resize(1, 13))
End If
End If
Next jZ
Next jJ
dRow = [F65432].End(xlUp).Row + 1
If dRow > 39 Then dRow = dRow + 1:
Rng.Copy Destination:=Range("B" & dRow): Range("A" & dRow) = sCot
RngC.Copy Destination:=Range("C" & dRow)
Set Rng = Nothing: Set RngC = Nothing
17 Next jW
lRow = [c65432].End(xlUp).Row + 1: dRow = 39
For jW = 39 To lRow
If Cells(jW, 4) = "" Then
Cells(jW, 2) = "Tong " & Right(Range("A" & Cells(jW, 1).End(xlUp).Row), 2)
Range("B" & jW & ":C" & jW).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.Bold = True
End With
Selection.Merge
cRow = jW - 1
Cells(jW, 4).Formula = "=SUM(D" & dRow & ":D" & cRow & ")"
Range("D" & jW).AutoFill Destination:=Range("D" & jW & _
":O" & jW), Type:=xlFillDefault
dRow = jW + 1
End If
Next jW
Range("A39:A" & lRow).Select: Selection.Font.Bold = True
End Sub