Function NameTypeSort(Data_Range As Range, TextName As Variant, TextType As Variant) As Variant
Dim Cell
Dim Current_Row As Integer
Dim No_Of_Rows_in_Range As Integer
Dim No_of_Cols_in_Range As Integer
Dim Matching_Row As Integer
Dim ColO As Integer
Dim ColTw As Integer
Dim ColTh As Integer
Dim TypeNameO As Variant
Dim TypeNameTw As Variant
Dim TypeNameTh As Variant
Dim NumberOne As Integer
Dim NumberTwo As Integer
Dim NumberThree As Integer
NameTypeSort = CVErr(xlErrNA)
Matching_Row = 0
Current_Row = 1
ColO = 2
ColTw = 3
ColTh = 4
No_Of_Rows_in_Range = Data_Range.Rows.Count
No_of_Cols_in_Range = Data_Range.Columns.Count
If (ColO > No_of_Cols_in_Range) Then
NameTypeSort = CVErr(xlErrRef)
End If
If (ColTw > No_of_Cols_in_Range) Then
NameTypeSort = CVErr(xlErrRef)
End If
If (ColTh > No_of_Cols_in_Range) Then
NameTypeSort = CVErr(xlErrRef)
End If
'==========================================
If (ColO <= No_of_Cols_in_Range) Then
Do
If (Data_Range.Cells(Current_Row, 1).Value = TextType) Then
Matching_Row = Current_Row
End If
Current_Row = Current_Row + 1
Loop Until ((Current_Row = No_Of_Rows_in_Range) Or (Matching_Row <> 0))
'==========================================
If Matching_Row <> 0 Then
TypeNameO = Data_Range.Cells(Matching_Row, ColO)
If TypeNameO > 0 Then
If InStr(Application.Substitute(TextName, TypeNameO, "", 1), TypeNameO) > 0 Then
NumberOne = 100
Else
If InStr(TextName, TypeNameO) > 0 Then
NumberOne = 1
Else
NumberOne = 0
End If
End If
End If
'==========================================
If (ColTw <= No_of_Cols_in_Range) Then
Do
If (Data_Range.Cells(Current_Row, 1).Value = TextType) Then
Matching_Row = Current_Row
End If
Current_Row = Current_Row + 1
Loop Until ((Current_Row = No_Of_Rows_in_Range) Or (Matching_Row <> 0))
'==========================================
If Matching_Row <> 0 Then
TypeNameTw = Data_Range.Cells(Matching_Row, ColTw)
If TypeNameTw > 0 Then
If InStr(Application.Substitute(TextName, TypeNameTw, "", 1), TypeNameTw) > 0 Then
NumberTwo = 100
Else
If InStr(TextName, TypeNameTw) > 0 Then
NumberTwo = 1
Else
NumberTwo = 0
End If
End If
End If
'==========================================
If (ColTh <= No_of_Cols_in_Range) Then
Do
If (Data_Range.Cells(Current_Row, 1).Value = TextType) Then
Matching_Row = Current_Row
End If
Current_Row = Current_Row + 1
Loop Until ((Current_Row = No_Of_Rows_in_Range) Or (Matching_Row <> 0))
'==========================================
If Matching_Row <> 0 Then
TypeNameTh = Data_Range.Cells(Matching_Row, ColTh)
If TypeNameTh > 0 Then
If InStr(Application.Substitute(TextName, TypeNameTh, "", 1), TypeNameTh) > 0 Then
NumberThree = 100
Else
If InStr(TextName, TypeNameTh) > 0 Then
NumberThree = 1
Else
NumberThree = 0
End If
End If
End If
'==========================================
NameTypeSort = CVar(NumberOne + NumberTwo + NumberThree)
Select Case NameTypeSort
Case 0
NameTypeSort = "NF"
Case 1
If NumberOne = 1 Then
NameTypeSort = TypeNameO
Else
If NumberTwo = 1 Then
NameTypeSort = TypeNameTw
Else
If NumberThree = 1 Then
NameTypeSort = TypeNameTh
End If
End If
End If
Case 2
NameTypeSort = "MDN"
Case 3
NameTypeSort = "MDN"
Case Is > 99
NameTypeSort = "MN"
End Select
End If
End If
End If
End If
End If
End If
End Function