'Is there a VBA worksheet function that will allow me to do a two layer search?

Is there any formula that I can use to make a double-layer search?

I want to be able to gather whatever information on the third column that meets two criteria.

  1. It has to be one location (in this case, Indonesia)
  2. It has to be a particular industry (FMCG)

Example here: enter image description here



Solution 1:[1]

If you want a VBA solution, please use the next function:

Function extractIndSize(strCountry As String, strInd As String, rng As Range) As Variant
    Dim i As Long, arr
    arr = rng.value
    For i = 1 To UBound(arr)
        If arr(i, 1) = strCountry And arr(i, 2) = strInd Then
            extractIndSize = arr(i, 3): Exit Function
        End If
    Next i
End Function

It can be tested using the next way:

Sub testExtractIndustrySize()
  Dim sh As Worksheet, lastR As Long, rng As Range
  
  Set sh = ActiveSheet
  lastR = sh.Range("A" & sh.rows.count).End(xlUp).Row
  Set rng = sh.Range("A2:C" & lastR)
  
  MsgBox extractIndSize("Indonesia", "FMCG", rng)
End Sub

The function can also be called (as UDF) from a cell, using the next formula:

 =extractIndSize(E2,F2,A2:C11)

Where in "E2" the country name must be introduced, in "F2" the industry name and the third parameter is the range to be processed (selected with the cursor),,,

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1