Option Explicit
Function DiemTB(BgDiem As Range, Chuyen As String, Optional TraVe As String = "TB")
Dim WF, Col As Byte, Diem As Double
Set WF = Application.WorksheetFunction
Col = Range("Mon").Find(Chuyen, , xlFormulas, xlWhole).Column - 3
Select Case TraVe
Case "TB", "XL", "TH"
Diem = (WF.Sum(BgDiem) + BgDiem.Cells(1, Col)) / (1 + BgDiem.Columns.Count)
If TraVe = "TB" Then
DiemTB = Diem
Else
If WF.Min(BgDiem) >= 5 Then
DiemTB = Switch(Diem < 5, "", Diem < 7, "TB", Diem < 9, "KHÁ", Diem <= 10, _
Range("TiengViet").Cells(4).Value)
If TraVe = "XL" Then Exit Function
If TraVe = "TH" And Len(DiemTB) > 2 And WF.Count(BgDiem) = BgDiem.Count Then
Diem = 10 ^ 5
If DiemTB = "KHÁ" Then DiemTB = Diem / 2 Else DiemTB = Diem
Else
DiemTB = ""
End If
End If
End If
Case "GC"
If WF.Min(BgDiem) >= 5 Then
DiemTB = Range("TiengViet").Cells(2).Value
ElseIf BgDiem.Cells(1, Col).Value < 5 Or WF.CountIf(BgDiem, "<5") > 1 Then
DiemTB = Range("TiengViet").Cells(3).Value
ElseIf 1 = WF.CountIf(BgDiem, "<5") Then
DiemTB = Range("TiengViet") '.Cells(1).Value
End If
Case "TL"
If 1 = WF.CountIf(BgDiem, "<5") And BgDiem.Cells(1, Col).Value >= 5 Then _
DiemTB = Range("Mon").Cells(1, BgDiem.Find(WF.Min(BgDiem)).Column - 3).Value
Case Else
DiemTB = "GPE.COM"
End Select
End Function