VBA INDEX MATCH Based on Multiple Criteria in Excel (3 Methods)

This is the sample dataset for illustration. We will extract a certain result residing in one column based on conditions from the other two columns.

Dataset for excel vba index match multiple criteria


Method 1 – Embed VBA with INDEX MATCH for Multiple (Two) Dimensional Lookup in Excel

We have stored a specific student’s name “Edge” in Cell G4; and the column that we will be searching the Result in, Exam Marks, is stored in Cell G5. We will search in the Exam Marks column and store the Marks that “Edge” got in Cell G6.

Steps:

  • Press Alt + F11 or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • In the pop-up code window, click Insert -> Module.

  • Copy the following code and paste it into the code window.
Sub IndexMatchStudent()
Dim iSheet As Worksheet
Set iSheet = Worksheets("Two Dimension")
iSheet.Range("G6").Value = Application.WorksheetFunction.Index(iSheet.Range("C5:D14"), Application.WorksheetFunction.Match(iSheet.Range("G4"), iSheet.Range("B5:B14"), 0), Application.WorksheetFunction.Match(iSheet.Range("G5"), iSheet.Range("C4:D4"), 0))
End Sub

Excel vba index match for multiple criteria with two dimensional lookup

  • Press F5 on your keyboard, or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Run icon in the sub-menu bar to run the macro.

The Marks that “Edge” got in the exam, 67 is retrieved in Cell G7.

Result of Excel vba index match for multiple criteria with two dimensional lookup

VBA Code Explanation

Dim iSheet As Worksheet

Defining the variable of Worksheet.

Set iSheet = Worksheets("Two Dimension")

Store the worksheet name. The name of our sheet is “Two Dimension”, you should provide the name according to your spreadsheet.

iSheet.Range("G6").Value = Application.WorksheetFunction.Index(iSheet.Range("C5:D14"), Application.WorksheetFunction.Match(iSheet.Range("G4"), iSheet.Range("B5:B14"), 0), Application.WorksheetFunction.Match(iSheet.Range("G5"), iSheet.Range("C4:D4"), 0))

This piece of code selects the range C5:D14 as the lookup range. Search for the match that is stored in cell G4 in range B5:B14 and search for the match that is stored in cell G5 in range C4:D4 and pass the result to cell G6.


Method 2 – Apply Macro to Find MATCH Value by INDEX with User-Defined Function (UDF)

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Enter the following code:
Function MatchByIndex(x As Double, y As Double)
    Const StartRow = 4
    Dim EndRow As Long
    Dim iRow As Long
    With Worksheets("UDF")
        EndRow = .Range("C:D").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For iRow = StartRow To EndRow
            If .Range("C" & iRow).Value = x And .Range("D" & iRow).Value = y Then
                MatchByIndex = .Range("B" & iRow).Value
                Exit Function
            End If
        Next iRow
    End With
    MatchByIndex = "Data Not found"
End Function

Excel VBA index match based on multiple criteria with UDF

  • Don’t run this code, save
  • Go back to the worksheet of interest. Pick any cell that you want to store the result. In our case, it is Cell F5.
  • In that cell, write the UDF you have just created in the code (MatchByIndex) and pass the Student ID and Exam Marks of the specific student inside the parentheses of the function.

As we are trying to extract the name “Finn” from his ID (105) and Marks (84), the formula becomes,

=MatchByIndex(105,84)
  • Press Enter.

Applying function for Excel VBA index match based on multiple criteria with UDF

In Cell F5, we have successfully retrieved the name “Finn” by passing his ID and Marks inside the function that we created in the VBA code.

Result of Excel VBA index match based on multiple criteria with UDF

VBA Code Explanation

Function MatchByIndex(x As Double, y As Double)

Creating a new function and passing the variables inside it. You can define any name to the function.

Const StartRow = 4

Our row starts from row number 4. You must provide the row number that your dataset starts from.

Dim EndRow As Long
Dim iRow As Long

Defining the variables.

With Worksheets("UDF")
    EndRow = .Range("C:D").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Define the worksheet to work with. The name of our sheet is “UDF”, you should provide the name according to your spreadsheet. Start searching in the range C:D from the first row that we defined to the last row.

For iRow = StartRow To EndRow
    If .Range("C" & iRow).Value = x And .Range("D" & iRow).Value = y Then
        MatchByIndex = .Range("B" & iRow).Value
      Exit Function
    End If
  Next iRow
End With

Start iterating from the first row to the last row. If the first value that we will pass inside the function falls inside the C column and if the second value that we will pass inside the function falls inside the D column, it will return the match from the B column. Otherwise, exit the function, end all the statements and go to the next line.

MatchByIndex = "Data Not found"
End Function

If the previous condition doesn’t get fulfilled while executing, the “Data Not Found” message will be returned and the code will leave the function.


Method 3 – Implement VBA to Return MATCH Value from a Table with Multiple Data in Excel

Let’s see how to extract the Marks from the table shown in our dataset (Table Name: TableMatch) of a certain student by providing the Name and the ID inside the code. For our case, the Name and the ID will be Finn and 105 respectively.

Steps:

  • Open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Enter the following code.
Sub ReturnMatchedResultByIndex()
  Dim iBook As Workbook
  Dim iSheet As Worksheet
  Dim iTable As Object
  Dim iValue As Variant
  Dim TargetName As String
  Dim TargetID As Long
  Dim IdColumn As Long
  Dim NameColumn As Long
  Dim MarksColumn As Long
  Dim iCount As Long
  Dim iResult As Boolean
  Set iBook = Application.ThisWorkbook
  Set iSheet = iBook.Sheets("Return Result")
  Set iTable = iSheet.ListObjects("TableMatch")
  TargetID = 105
  TargetName = "Finn"
  IdColumn = iTable.ListColumns("Student ID").Index
  NameColumn = iTable.ListColumns("Student Name").Index
  MarksColumn = iTable.ListColumns("Exam Marks").Index
  iValue = iTable.DataBodyRange.Value
  For iCount = 1 To UBound(iValue)
    If iValue(iCount, IdColumn) = TargetID Then
      If iValue(iCount, NameColumn) = TargetName Then
        iResult = True
      End If
    End If
    If iResult Then Exit For
  Next iCount
  If iResult Then
    MsgBox "ID: " & TargetID & vbLf & "Name: " & TargetName & vbLf & "Marks: " & iValue(iCount, MarksColumn)
  Else
    MsgBox "ID: " & TargetID & vbLf & "Name: " & TargetName & vbLf & "Marks Not found"
  End If
End Sub

Excel vba index match based on multiple criteria for table in mxgbox

  • Run the code.There is a Microsoft Excel pop-up message box showing you the Marks: 84 of ID: 105 and Name: Finn that we provided inside the code.

Result of excel vba index match based on multiple criteria for table in mxgbox

VBA Code Explanation

Dim iBook As Workbook
Dim iSheet As Worksheet
Dim iTable As Object
Dim iValue As Variant
Dim TargetName As String
Dim TargetID As Long
Dim IdColumn As Long
Dim NameColumn As Long
Dim MarksColumn As Long
Dim iCount As Long
Dim iResult As Boolean

Defining the variables.

Set iBook = Application.ThisWorkbook
Set iSheet = iBook.Sheets("Return Result")
Set iTable = iSheet.ListObjects("TableMatch")

Setting the sheet name and the table name inside variables.

TargetID = 105
TargetName = "Finn"
IdColumn = iTable.ListColumns("Student ID").Index
NameColumn = iTable.ListColumns("Student Name").Index
MarksColumn = iTable.ListColumns("Exam Marks").Index

Storing the lookup values and the lookup columns to search.

  iValue = iTable.DataBodyRange.Value
  For iCount = 1 To UBound(iValue)
    If iValue(iCount, IdColumn) = TargetID Then
      If iValue(iCount, NameColumn) = TargetName Then
        iResult = True
      End If
    End If
If iResult Then Exit For
Next iCount

This piece of code scans through from the start to the end of the subscript and if it finds the match of the defined ID and the Name in the search columns then store the result and close all the statements. Exit the iteration and go to the next part of the code.

If iResult Then
  MsgBox "ID: " & TargetID & vbLf & "Name: " & TargetName & vbLf & "Marks: " & iValue(iCount, MarksColumn)
Else
  MsgBox "ID: " & TargetID & vbLf & "Name: " & TargetName & vbLf & "Marks Not found"
End If

Throws the result in the MsgBox.


Download Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

2 Comments
  1. Can I contact Administration?
    I’ts important.
    Regards.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo