excelvba

search within rows and columns based on two search criteria and select a single cell vba


The image below is for illustration purposes only.

enter image description here

I am using this VBA code, But it support only Single search criteria.

Is there any way to apply both search criteria to select related cell? Because both search criteria can change every time.

Sub Find_Items()
Dim rFound As Range

  Set rFound = ActiveSheet.Columns(2).Find(What:=ActiveSheet.Range("L3").Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
  
  If Not rFound Is Nothing Then
  
   rFound.Offset(0, 3).Select
End If
End Sub

Solution

  • Using WorksheetFunction functions this is the code:

    Sub twocrit()
    Set res = Nothing
    On Error Resume Next
    Set res = WorksheetFunction.Index(Range("C3:G12"), WorksheetFunction.Match(Range("L3"), Range("B3:B12"), 0), WorksheetFunction.Match(Range("L4"), Range("C2:G2"), 0))
    On Error GoTo 0
    If res Is Nothing Then MsgBox ("Not valid input!"): Exit Sub
    res.Select
    Range("L7") = res.Address
    End Sub