Sub Taoso()
Dim i As Long, Rw As Long, t As Double
Dim Chungtu As Range, Ci8 As Range, Ci9 As Range
Dim DK1 As Boolean, Dk2 As Boolean, Dk3 As Boolean, Dk4 As Boolean
Application.ScreenUpdating = False
t = Timer
Range("A7:I65536").ClearContents
With Sheets("DATA")
Rw = .[D65536].End(xlUp).Row
Set Chungtu = .Range("A7:A" & Rw)
End With
With Chungtu
.Resize(, 4).Copy Destination:=[A7]
.Offset(, 6).Copy Destination:=[G7]
.Offset(, 4).Resize(, 2).Copy Destination:=[H7]
End With
Set Chungtu = Nothing
For i = 7 To [D65536].End(xlUp).Row
With Cells(i, 5)
Set Ci8 = .Offset(, 3): Set Ci9 = .Offset(, 4)
DK1 = ((Cells(i, 1) = Cells(i + 1, 1)) * (.Offset(, 3) > 0)) = 1
Dk2 = ((Cells(i, 1) = Cells(i - 1, 1)) * (.Offset(, 3) > 0)) = 1
Dk3 = ((Cells(i, 1) = Cells(i + 1, 1)) * (.Offset(, 4) > 0)) = 1
Dk4 = ((Cells(i, 1) = Cells(i - 1, 1)) * (.Offset(, 4) > 0)) = 1
'-------------------------------------------------------------------------------------
'1a.> 1 No 1 Co voi No truoc
If DK1 And Ci8 = Cells(i + 1, 9) Then
.Value = Cells(i + 1, 4)
'1b.> 1 No 1 Co voi Co truoc
ElseIf Dk2 And Ci8 = Cells(i - 1, 9) Then
.Value = Cells(i - 1, 4)
'2a.> 2 No 1 Co voi No truoc
ElseIf DK1 And Ci8 + Cells(i + 1, 8) = Cells(i + 2, 9) Then
.Value = Cells(i + 2, 4)
ElseIf DK1 And Ci8 + Cells(i - 1, 8) = Cells(i + 1, 9) Then
.Value = Cells(i + 1, 4)
'2b.> 2 No 1 Co voi Co truoc
ElseIf Dk2 And Ci8 + Cells(i + 1, 8) = Cells(i - 1, 9) Then
.Value = Cells(i - 1, 4)
ElseIf Dk2 And Ci8 + Cells(i - 1, 8) = Cells(i - 2, 9) Then
.Value = Cells(i - 2, 4)
'3a.> 3 No 1 Co voi No truoc
ElseIf DK1 And Ci8 + Cells(i + 1, 8) + Cells(i + 2, 8) = Cells(i + 3, 9) Then
.Value = Cells(i + 3, 4)
ElseIf DK1 And Ci8 + Cells(i - 1, 8) + Cells(i + 1, 8) = Cells(i + 2, 9) Then
.Value = Cells(i + 2, 4)
ElseIf DK1 And Ci8 + Cells(i - 1, 8) + Cells(i - 2, 8) = Cells(i + 1, 9) Then
.Value = Cells(i + 1, 4)
'3b.> 3 No 1 Co voi Co truoc
ElseIf DK1 And Ci8 + Cells(i + 1, 8) + Cells(i + 2, 8) = Cells(i - 1, 9) Then
.Value = Cells(i - 1, 4)
ElseIf DK1 And Ci8 + Cells(i + 1, 8) + Cells(i - 1, 8) = Cells(i - 2, 9) Then
.Value = Cells(i - 2, 4)
ElseIf Dk2 And Ci8 + Cells(i - 1, 8) + Cells(i - 2, 8) = Cells(i - 3, 9) Then
.Value = Cells(i - 3, 4)
'Tu truong hop 4a tro ve sau thuat toan se khac, do la dao chieu giua No va Co
'4a.> 1 No 2 Co voi No truoc
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) = Cells(i - 1, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(-1, -1)
ElseIf Dk4 And Ci9 + Cells(i - 1, 9) = Cells(i - 2, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(-2, -1)
'4b.> 1 No 2 Co voi Co truoc
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) = Cells(i + 2, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(2, -1)
ElseIf Dk3 And Cells(i - 1, 9) <> 0 And Ci9 + Cells(i - 1, 9) = Cells(i + 1, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(1, -1)
'5a.> 1 No 3 Co voi No truoc
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) + Cells(i + 2, 9) = Cells(i - 1, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(-1, -1)
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) + Cells(i - 1, 9) = Cells(i - 2, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(-2, -1)
ElseIf Dk4 And Cells(i - 1, 9) <> 0 And Ci9 + Cells(i - 1, 9) + Cells(i - 2, 9) = Cells(i - 3, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(-3, -1)
'5b.> 1 No 3 Co voi Co truoc
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) + Cells(i + 2, 9) = Cells(i + 3, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(3, -1)
ElseIf Dk3 And Cells(i + 1, 9) <> 0 And Ci9 + Cells(i + 1, 9) + Cells(i - 1, 9) = Cells(i + 2, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(2, -1)
ElseIf Dk3 And Cells(i - 1, 9) <> 0 And Ci9 + Cells(i - 1, 9) + Cells(i - 2, 9) = Cells(i + 1, 8) Then
.Value = .Offset(, -1)
.Offset(, -1) = .Offset(1, -1)
End If
'-------------------------------------------------------------------------------------
If .Value <> "" And .Offset(, -1) <> "" Then .Offset(, 1).Value = Ci8 + Ci9
End With
Next i
Set Ci8 = Nothing: Set Ci9 = Nothing
DelRow ' Huy dong lenh nay de xem va sua chua cac thuat toan o tren
[A2] = Timer - t
[A6:G6].AutoFilter
Application.ScreenUpdating = True
End Sub
'=============================================================================================
Sub DelRow()
Dim Cell As Range, Rng As Range, r As Long
Set Rng = Range("F7:F" & [D65536].End(xlUp).Row)
For Each Cell In Rng
Cell.Offset(, 2).Value = Cell.Row
If Cell.Value = 0 Then Cell.EntireRow.Clear
Next
Range("A7:H65536").Sort key1:=[H7], order1:=xlAscending
[D7:D65536].HorizontalAlignment = xlCenter
[E7:E65536].HorizontalAlignment = xlCenter
[G7:G65536].HorizontalAlignment = xlCenter
[F7:F65536].NumberFormat = "#,##0"
Columns("H:I").Clear
Set Rng = Nothing: Set Cell = Nothing
End Sub