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

When we have a large amount of data then sometimes it is hard to extract any specific data from the dataset. Together with Excel’s INDEX and MATCH functions can retrieve any kind of data even in a huge dataset. Implementing VBA is the most effective, quickest, and safest method to run any operation in Excel. In this article, we will show you 3 different methods on how to perform INDEX MATCH based on multiple criteria in Excel with the VBA macro.


VBA INDEX MATCH Based on Multiple Criteria in Excel: 3 Methods

In the following sections, we will show you how to perform INDEX MATCH based on multiple criteria for a range, for a specific selection and for a table in Excel with VBA.

Dataset for excel vba index match multiple criteria

Above we have the dataset that this article will follow. We have the Student Name, Student ID, and Exam Marks of each student in the dataset. We will extract a certain result residing in one column based on conditions from the other two columns.


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

Consider the following image. 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.

The steps to lookup result in a two-dimensional array with INDEX and MATCH in Excel with VBA are given below.

Steps:

  • In the beginning, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • Next, in the pop-up code window, from the menu bar, click Insert -> Module.

  • Then, 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

Your code is now ready to run.

Excel vba index match for multiple criteria with two dimensional lookup

  • Now, 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.

After the code execution, look at the gif below to see the result.

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

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

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. Then 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.


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

You can extract matched values from a dataset with a user-defined function (UDF). From the following image, what we are going to do is, we will pass the Student ID and Exam Marks of a certain student and the function will throw us the Name of that specific student.

Let’s see how to achieve that for Student Name “Finn” with VBA.

Steps:

  • As shown before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, in the code window, copy the following code and paste it.
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 it.
  • Now, 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), so for our case the formula becomes,

=MatchByIndex(105,84)
  • Then, press Enter.

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

Look at the following image.

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

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

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

First, define the worksheet to work with. The name of our sheet is “UDF”, you should provide the name according to your spreadsheet. Then 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, then 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, then the “Data Not Found” message will be returned and the code will leave the function.


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

In this section, we will learn how to return a matched value by the indexes from a table in the MsgBox in VBA 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:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
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

Your code is now ready to run.

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

  • Later, Run this code and look at the following image to see what happened as a result.

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

As you can see from the above image, 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.

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. Also, 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


Conclusion

To conclude, this article showed you 3 different methods on how to perform INDEX MATCH based on multiple criteria in Excel with the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic.


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

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo