Home » excel » excel – VBA – Index / Match function with multiple criteria

excel – VBA – Index / Match function with multiple criteria

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am facing an issue when trying to compile a multiple criteria Index/Match code in VBA. This might be simple – but i am fairly new to VBA and nothing i have found around here worked.

Example:
I have a large amount of data in a specified range: Sheets("CustomerAccounts").Range(CustomerSheetRange)) – I need VBA to return data from column titled “Values” by checking three criteria: Customer = X, Type = External, OriginCountry = UAE (columns are not adjacent in the original spreadsheet)
The criteria are stored in separate variables set by user of the macro beforehand.

Customer   | Type      | Origin        | Destination        |  Values
X          | Internal  | UAE           |  SA                |  Value 1
Y          | Internal  | UAE           |  SA                |  Value 2
X          | External  | UAE           |  SA                |  Value 3
X          | External  | ZA            |  UAE               |  Value 4

At the moment i have the following (quite bulky) code which finds the value using one criteria – OriginCountry variable.
The code searches for it in a pre-specified column – OriginCountryColumn.

ResultString = Application.Index(Sheets("CustomerAccounts").Range(CustomerSheetRange), Application.Match(OriginCountry, Sheets("CustomerAccounts").Range(OriginCountryColumn), 0), Application.Match("Values", Sheets("CustomerAccounts").Range(TitleRowCust), 0))

I would like to modify the code to also match the Type and The customer.
Is it possible to expand the above Index/Matxh function – or should i use a different approach?

Any advice is appreciated.

How to&Answers:

You may walk through rows checking matches:

Dim row as Long
With Sheets("CustomerAccounts").Range(CustomerSheetRange))
    For row = 2 To .Rows.Count 'Starts in 2 to ignore header!
        If .Cells(row, costumerCol).Value Like costumerCriteria And .Cells(row, typeCol).Value Like typeCriteria And .Cells(row, originCol).Value Like originCriteria Then
            'This is a match!
            Debug.Print .Cells(row, valueCol)
        End if
    Next
End With

You must replace costumerCol, typeCol, originCol and valueCol with corresponding column number and costumerCriteria, typeCriteria and originCriteria with criteria specified.

If column indexes are also variable, make a search for them in first row before walking through rows.

Answer:

First, format the range containing your data to a Table (See http://office.microsoft.com/en-001/excel-help/quick-start-create-an-excel-table-HA010359200.aspx on how to do that). Once done, use the following VBA code:

SomeCustomer = Range("...").Value
SomeType = Range("...").Value
SomeOrigin = Range("...").Value
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria=SomeCustomer
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=2, Criteria=SomeType
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=3, Criteria=SomeOrigin

Notes:

  • You might have to customize this macro for your specific needs
  • Name of table can be found/modified through Formulas>Name Manager
  • ActiveSheet might be modified to the actual sheet you are using