VBA - lookup range with 2 conditions

davidhasselhoff

New Member
Joined
Feb 12, 2009
Messages
37
Hello!

I'm using the following function to lookup a value based on two conditions. It works perfectly if I try to find only one value. However, I would like it to give me a range of corresponding values if there are more than one.

Does anybody here know how I could do so?
I didn't invent this code and hardly understand it, so I can't imagine a way to do this myself.

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

Any help would be greatly appreciated!

Marc
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top