Jamesbutt84
New Member
- Joined
- Nov 29, 2017
- Messages
- 5
Hi all,
I have been attempting to find a good way to perform a vlookup to return up to 14 results for the same lookup value and have been trying a number of possible combinations of INDEX & MATCH formulas, but found the results to be inconsistent (possibly through my own error) so settled on the below VBA code for LOOKUPNTH:
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 = "Not Found"
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
This has done the trick nicely, but it is slowing my system up immensely when it is trying to do the calculations (often 20 minutes to half an hour to caluculate!)
Does anyone know either how to optimise the above or able to provide a better solution for this?
Any help would be greatly appreciated!
Many thanks,
James
I have been attempting to find a good way to perform a vlookup to return up to 14 results for the same lookup value and have been trying a number of possible combinations of INDEX & MATCH formulas, but found the results to be inconsistent (possibly through my own error) so settled on the below VBA code for LOOKUPNTH:
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 = "Not Found"
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
This has done the trick nicely, but it is slowing my system up immensely when it is trying to do the calculations (often 20 minutes to half an hour to caluculate!)
Does anyone know either how to optimise the above or able to provide a better solution for this?
Any help would be greatly appreciated!
Many thanks,
James