• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need thing like lookup but result depends on two columns not on one

Flowerski

New Member
Dear experts
I need vba code or excel function that works like lookup with small change.
Need something with syntax: lookupvalue, lookuparray1, lookuparray2, lookup array result. My problem briefly is as follows: big database with - column 1 with dates, column 2 with certain parameters and column 3 with another parameters - see example:
Date Parameter1 Parameter2
5/2/2061 Buy Company1
5/3/2016 Buy Company2
5/4/2016 Sell Company1
5/5/2016 Start Company1
5/6/2016 Start Company2
5/7/2016 Start Company3

Need newlookup with syntax: lookupvalue=Start, lookuparray1=Parameter1, lookuparray2=Parameter2, lookuparray result = Date. The result should be date = 5/5/2016. The above database has few thousands rows in reality. Each company has assigned one Start parameter. Any idea ?
Flowerski
 
You can use this array formula
Code:
=INDEX($A$1:$A$10,MATCH(1,($B$1:$B$10="Start")*($C$1:$C$10="Company1"),0))
 
You could use this UDF
Code:
Function FindMatch(X As Variant, Y As Variant)
    Dim LastRow    As Long
    Dim CurRow      As Long
    Const FirstRow = 1
   
    With Worksheets("Sheet1")
        LastRow = .Range("A:A").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       
        For CurRow = FirstRow To LastRow
            If .Range("B" & CurRow).Value = X And _
              .Range("C" & CurRow).Value = Y Then
                FindMatch = .Range("A" & CurRow).Value
                Exit Function
            End If
        Next CurRow
    End With

    FindMatch = "Not Found"
End Function

Code:
=FindMatch("Start","Company1")
 
You could use this UDF
Code:
Function FindMatch(X As Variant, Y As Variant)
    Dim LastRow    As Long
    Dim CurRow      As Long
    Const FirstRow = 1
  
    With Worksheets("Sheet1")
        LastRow = .Range("A:A").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      
        For CurRow = FirstRow To LastRow
            If .Range("B" & CurRow).Value = X And _
              .Range("C" & CurRow).Value = Y Then
                FindMatch = .Range("A" & CurRow).Value
                Exit Function
            End If
        Next CurRow
    End With

    FindMatch = "Not Found"
End Function

Code:
=FindMatch("Start","Company1")

Dear Great Man YasserKhalil. I did not try your code on spread sheet yet but I can see that it analyzes row by row. My DB has many thousands rows and I am looking for something that work fast on big ranges; something like sumifs, countifs etc. Is there a combination of VBA formulas to solve my problem in fast fashion? Thanks for your code. I will study it as it is written in the way I have never practiced. Thanks
 
Back
Top