Option Explicit
Sub TongHop()
Const GDoan As String = "Giai Doan "
Dim lRow As Long, Wzj As Long: Dim GPE As Byte
Dim Clls As Range: Dim sTen As String, MChinh As String
1 ' Lap DS Giao Vien Day Cac Mon Hoc Tai "S1"'
lRow = Sheets("LocGV6").[b65500].End(xlUp).Row
Application.ScreenUpdating = False
With Sheets("S1")
.Range("A1:z" & lRow - 3) = Sheets("LocGV6").Range("A4:Z" & lRow).Value
.[a2] = "TT": .[b2] = "HTenGV": .[c2] = "MonHoc"
.[d2] = "Lop": .[E2] = "SiSo"
For GPE = 1 To 4
Set Clls = Choose(GPE, .[f1], .[k1], .[p1], .[U1])
Clls = GDoan & Choose(GPE, "I", "II", "III", "IV")
Clls.Font.Bold = True: Clls.Resize(, 3).Merge
Next GPE
.Range("B2:W" & lRow).Sort Key1:=.Range("B3"), Order1:=xlAscending, _
Key2:=.Range("C3"), Order2:=xlAscending, Key3:=.Range("D3"), _
Order3:=xlAscending, Header:=xlYes, OrderCustom:=1
.Range("A1:E1") = "":
GPE = 0: .Range("A4:A" & lRow).Clear
For Wzj = 3 To lRow
With .Cells(Wzj, "B")
If .Value = "" Then Exit For
.Offset(, -1) = Wzj - 2
If .Value = sTen Or .Value = .Offset(-1) Then
.Value = ""
Else
sTen = .Value
End If
End With
Next Wzj
2 ' Thay Ma Mon Hoc "VNTNLYSISUDDAH"'
lRow = .[c65500].End(xlUp).Row: Sheets("Luu").Select
For Wzj = lRow To 3 Step -1
Set Clls = Range("B2:B15").Find(what:=.Cells(Wzj, "C"), LookIn:=xlFormulas)
If Not Clls Is Nothing Then .Cells(Wzj, "C") = Clls.Offset(, -1)
Next Wzj
MChinh = "VNTN": sTen = "SISUDDAN"
Sheets("S1").Select
For Wzj = 3 To lRow + 1
With .Cells(Wzj, "B")
3 ' Tong Hop Voi Nhung GV Chi Day 01 Mon Hoc'
If .Value <> "" And .Offset(1) <> "" And .Offset(1) <> .Value Or Wzj = lRow Then
For Each Clls In Union(.Offset(, 4), .Offset(, 9), .Offset(, 14), .Offset(, 19))
If Clls <> "" Then
Clls.Offset(, 3) = Clls.Value: Clls.Offset(, 4) = Clls.Offset(, 2).Value
End If
Next Clls
End If
4 ' Tong Hop Voi Nhung GV Day 02 Mon Hoc'
If .Value <> "" And .Offset(1) = "" And .Offset(2) <> "" Then
41 ' Truong Hop 02 Mon Dieu La Mon Hoc Phu Hay Chinh'
If InStr(MChinh, .Offset(, 1)) > 0 And InStr(MChinh, .Offset(1, 1)) > 0 Or _
InStr(sTen, .Offset(, 1)) > 0 And InStr(sTen, .Offset(1, 1)) > 0 Then
For Each Clls In Union(.Offset(, 4), .Offset(, 9), .Offset(, 14), .Offset(, 19))
If Clls <> "" Then
Clls.Offset(, 3) = (Clls.Value + Clls.Offset(1)) / 2
Clls.Offset(, 4) = (Clls.Offset(, 2).Value + Clls.Offset(1, 2)) / 2
End If
Next Clls
End If
42 ' Truong Hop 01 Mon Hoc La Phu Con Mon Kia La Chinh'
421 ' Truong Hop Mon Hoc Tren La Phu'
If InStr(sTen, .Offset(, 1)) > 0 And InStr(MChinh, .Offset(1, 1)) > 0 Then
For Each Clls In Union(.Offset(, 4), .Offset(, 9), .Offset(, 14), .Offset(, 19))
If Clls <> "" Then
Clls.Offset(, 3) = (Clls.Value + 2 * Clls.Offset(1)) / 3
Clls.Offset(, 4) = (Clls.Offset(, 2).Value + 2 * Clls.Offset(1, 2)) / 3
Clls.Offset(, 3).Resize(, 2).NumberFormat = "0.00"
ElseIf Clls = "" And Clls.Offset(1) <> "" Then
Clls.Offset(1, 3) = Clls.Offset(1)
Clls.Offset(1, 4) = Clls.Offset(1, 2).Value '*'
End If
Next Clls
422 ' Truong Hop Mon Hoc Tren La Chinh'
ElseIf InStr(MChinh, .Offset(, 1)) > 0 And InStr(sTen, .Offset(1, 1)) > 0 Then
For Each Clls In Union(.Offset(, 4), .Offset(, 9), .Offset(, 14), .Offset(, 19))
If Clls <> "" Then
Clls.Offset(, 3) = (Clls.Value * 2 + Clls.Offset(1)) / 3
Clls.Offset(, 4) = (Clls.Offset(, 2).Value * 2 + Clls.Offset(1, 2)) / 3
Clls.Offset(, 3).Resize(, 2).NumberFormat = "0.00"
End If
Next Clls
End If
End If
5 ''
End With
Next Wzj
.Columns("A:Z").EntireColumn.AutoFit
End With
End Sub