methodXD
9/3/2013 - 11:54 PM

Function: Finds nth item in VLOOKUP function

Function: Finds nth item in VLOOKUP function

Function VLOOKUPNTH(lookup_value, table_array As Range, _
    col_index_num As Integer, nth_value)
     ' Extension to VLOOKUP function.  Allows for finding
     ' the   "  nth  "   item that matches the lookup value.
     
    Dim nRow As Long
    Dim nVal As Integer
    Dim bFound As Boolean
    VLOOKUPNTH = "-"
    With table_array
        For nRow = 1 To .Rows.Count
            If .Cells(nRow, 1).Value = lookup_value Then
                nVal = nVal + 1
            End If
            If nVal = nth_value Then
                VLOOKUPNTH = .Cells(nRow, col_index_num).Text
                Exit Function
            End If
        Next nRow
    End With
End Function