Function GiaTriTrung(ByVal ResRng As Range, ByVal VungDk1 As Range, ByVal Dk1 As Range, Optional ByVal VungDk2 As Range = Nothing, Optional ByVal Dk2 As Range = Nothing, _
Optional ByVal VungDk3 As Range = Nothing, Optional ByVal Dk3 As Range = Nothing, Optional ByVal VungDk4 As Range = Nothing, Optional ByVal Dk4 As Range = Nothing, _
Optional ByVal VungDk5 As Range = Nothing, Optional ByVal Dk5 As Range = Nothing, Optional ByVal VungDk6 As Range = Nothing, Optional ByVal Dk6 As Range = Nothing, _
Optional ByVal VungDk7 As Range = Nothing, Optional ByVal Dk7 As Range = Nothing, Optional ByVal VungDk8 As Range = Nothing, Optional ByVal Dk8 As Range = Nothing _
) As String
'ViTriTrung(Vùng lay ket qua, Vùng dieu kien 1, Dieu kien 1, Vùng dieu kien 2, Dieu kien 2, Vùng dieu kien 3, Dieu kien 3 .... )
'Vùng lay ket qua và các Vùng dieu kien là Range
Dim i As Byte, j As Byte, n As Byte, nS As Byte
Dim dkTrue As Boolean, Arr(), VungDK As Variant, DK As Variant
VungDK = Array(VungDk1, VungDk2, VungDk3, VungDk4, VungDk5, VungDk6, VungDk7, VungDk8)
DK = Array(Dk1, Dk2, Dk3, Dk4, Dk5, Dk6, Dk7, Dk8)
ReDim Arr(LBound(DK) To UBound(DK))
For n = LBound(VungDK) To UBound(VungDK)
If Not VungDK(n) Is Nothing And Not DK(n) Is Nothing Then
Arr(n) = "|"
For i = 1 To DK(n).Rows.Count
For j = 1 To DK(n).Columns.Count
If Len(DK(n)(i, j)) Then Arr(n) = Arr(n) & DK(n)(i, j) & "|"
Next j
Next i
Else
nS = n - 1: Exit For
End If
Next n
For i = 1 To ResRng.Rows.Count
For j = 1 To ResRng.Columns.Count
dkTrue = True
For n = LBound(Arr) To nS
If InStr(Arr(n), "|" & VungDK(n)(i, j) & "|") = 0 Then dkTrue = False: Exit For
Next n
If dkTrue = True Then GiaTriTrung = GiaTriTrung & ResRng(i, j)
Next j
Next i
If Len(GiaTriTrung) = 0 Then GiaTriTrung = "Not Found"
End Function