Function XepLoai(Tiet1 As Range, Tiet2 As Range, Tiet3 As Range, SoTiet As Byte, Optional Loai As String = "") As Integer
On Error Resume Next
If Tiet1.Rows.Count <> Tiet2.Rows.Count Or Tiet2.Rows.Count <> Tiet3.Rows.Count Then Exit Function
Application.Volatile (False)
Dim i As Integer
Dim SoLan As Integer
For i = 1 To Tiet1.Rows.Count
SoLan = 0
Select Case SoTiet
Case 1
If Tiet1(i) <> 0 And Tiet3(i) = 0 And Tiet2(i) = 0 Then
If Tiet1(i) = Loai Then SoLan = 1
GoTo TimThay
End If
If Tiet2(i) <> 0 And Tiet1(i) = 0 And Tiet3(i) = 0 Then
If Tiet2(i) = Loai Then SoLan = 1
GoTo TimThay
End If
If Tiet3(i) <> 0 And Tiet2(i) = 0 And Tiet1(i) = 0 Then
If Tiet3(i) = Loai Then SoLan = 1
GoTo TimThay
End If
Case 2
If Tiet1(i) <> 0 And Tiet3(i) <> 0 And Tiet2(i) = 0 Then
If Tiet1(i) = Loai Then SoLan = 1
If Tiet3(i) = Loai Then SoLan = SoLan + 1
GoTo TimThay
End If
If Tiet2(i) <> 0 And Tiet1(i) <> 0 And Tiet3(i) = 0 Then
If Tiet1(i) = Loai Then SoLan = 1
If Tiet2(i) = Loai Then SoLan = SoLan + 1
GoTo TimThay
End If
If Tiet3(i) <> 0 And Tiet2(i) <> 0 And Tiet1(i) = 0 Then
If Tiet2(i) = Loai Then SoLan = 1
If Tiet3(i) = Loai Then SoLan = SoLan + 1
GoTo TimThay
End If
Case 3
If Tiet1(i) <> 0 And Tiet2(i) <> 0 And Tiet3(i) <> 0 Then
If Tiet1(i) = Loai Then SoLan = 1
If Tiet2(i) = Loai Then SoLan = SoLan + 1
If Tiet3(i) = Loai Then SoLan = SoLan + 1
GoTo TimThay
End If
End Select
GoTo Tiep
TimThay:
XepLoai = XepLoai + IIf(Loai = "", 1, SoLan)
Tiep:
Next
End Function