Let's create the following function in a new Excel module:
Function FindValue(rng1 As Range, rng2 As Range) As Variant
Dim varVal1 As Variant
Dim varVal2 As Variant
Dim rngTargetA As Range
Dim rngTargetB As Range
Dim lngRowCounter As Long
Dim ws As Worksheet
varVal1 = rng1.Value
varVal2 = rng2.Value
Set ws = ActiveSheet
lngRowCounter = 2
Set rngTargetA = ws.Range("A" & lngRowCounter)
Set rngTargetB = ws.Range("B" & lngRowCounter)
Do While Not IsEmpty(rngTargetA.Value)
If rngTargetA.Value = varVal1 And rngTargetB.Value = varVal2 Then
FindValue = ws.Range("C" & lngRowCounter).Value
Exit Function
End If
lngRowCounter = lngRowCounter + 1
Set rngTargetA = ws.Range("A" & lngRowCounter)
Set rngTargetB = ws.Range("B" & lngRowCounter)
Loop
' if we don't find anything, return an empty string '
FindValue = ""
End Function
The above function takes in two range values, so you can use it like you would use any other function in Excel. Using the example you provided above, copy those cells into cells A2:C5. Next, in cell A1 put A
. In cell B1 put 1
. In C1, put =FindValue(A1,B1)
. This will execute the code above and return a match if it finds it.
Furthermore, if you change the "input values" from cells A1 or B1, your answer will update accordingly.