davidhasselhoff
New Member
- Joined
- Feb 12, 2009
- Messages
- 37
Hi everyone!
I got this function I found on the internet and I use it to look up a value in a table based on two conditions, kind of a sumproduct for vba...
However, now I would require it to work with an additional condition...
So does somebody of you know how the following function works (because I don't ), and could he/she please tell me how I could add a third condition?
I got this function I found on the internet and I use it to look up a value in a table based on two conditions, kind of a sumproduct for vba...
However, now I would require it to work with an additional condition...
So does somebody of you know how the following function works (because I don't ), and could he/she please tell me how I could add a third condition?
Code:
Public Function BLOOKUP(first_lookup_value As Variant, _
first_col As Range, _
second_lookup_value As Variant, _
second_col As Range, _
return_col As Range, _
Optional NA_value As Variant = CVErr(xlErrNA)) As Variant
'Like Vlookup, but can take two input values
Application.Volatile (False)
Dim CriteriaOffset As Long
Dim ReutrnOffset As Long
Dim cell As Range
If first_col.Parent Is second_col.Parent = False Then Exit Function
If first_col.Parent Is return_col.Parent = False Then Exit Function
BLOOKUP = NA_value
Set first_col = Intersect(first_col, first_col.Parent.UsedRange)
If first_col Is Nothing Then Exit Function
Set second_col = Intersect(second_col, second_col.Parent.UsedRange)
If second_col Is Nothing Then Exit Function
CriteriaOffset = second_col.Column - first_col.Column
ReutrnOffset = return_col.Column - first_col.Column
For Each cell In Union(first_col.Cells(1), first_col.Columns(1))
If UCase(cell) = UCase(first_lookup_value) Then
If UCase(cell.Offset(0, CriteriaOffset)) = UCase(second_lookup_value) Then
BLOOKUP = cell.Offset(0, ReutrnOffset)
Exit For
End If
End If
Next cell
End Function