Yes, you read the title correctly; this is all about using more than one value in Excel’s VLOOKUP worksheet function. Well, that is partially true. Everyone knows that VLOOKUP , and it’s counterpart HLOOKUP, only accepts one value to lookup in a range. The problem was that one of my retail clients had a need for something more.
I was called upon to automate data collection and reporting for a major retailer with an office in Times Square in New York City. It was something I had done many times before but I wasn’t prepared for what was coming my way. I have consolidated data from multiple spreadsheets before (see Proactive Customer Service) but that was “clean” data, what I was dealing with on this project was anything but clean. They were importing many workbooks from retail outlets and generating reports from that data. While the same data was contained in each workbook they couldn’t count on the layout being consistent. They needed to be able to use three (3) criteria to find data in a table. You immediately think of either vLookUp or hLookUp but they only accept a single value. The idea of making Excel do something it doesn’t want to do was not new to me, but I had never done anything like this before. I fooled around with some of Excel’s native functions to see if I could get some combination to work but ultimately decided the best solution was a custom function written in VBA. The function accepts the following parameters:
- Table_Range
- Return_Col
- Col1_Fnd
- Col2_Fnd
- Col3_Fnd
Note that there is no “true” or “false” parameter – this function only finds exact matches. The “Table_Range” can be supplied as either a range (B3:L11) or as a name (retailData), as shown in the example. I also made the function case-insensitive but you could easily change this by removing “UCase” from the evaluation process.
I have a screenshot of a workbook shown here to illustrate how the function works. The formula is cell B1 is: =vlookup3criteria(retailData,8,”C”,3,”e”)
Function vlookup3criteria(Table_Range As Range, Return_Col As Long, Col1_Fnd, Col2_Fnd, Col3_Fnd)
Dim rCheck As Range, bFound As Boolean, lLoop As Long
On Error Resume Next
Set rCheck = Table_Range.Columns(1).Cells(1, 1)
With WorksheetFunction
For lLoop = 1 To .CountIf(Table_Range.Columns(1), Col1_Fnd)
Set rCheck = Table_Range.Columns(1).Find(Col1_Fnd, rCheck, xlValues, xlWhole, xlNext, xlRows, False)
If UCase(rCheck(1, 2)) = UCase(Col2_Fnd) And UCase(rCheck(1, 3)) = UCase(Col3_Fnd) Then
bFound = True
Exit For
End If
Next lLoop
End With
If bFound = True Then
vlookup3criteria = rCheck(1, Return_Col)
Else
vlookup3criteria = 0
End If
End Function
So, there you have it! You can use this function as-is or modify it to meet your needs. I actually wrote two (2) additional, similar, functions for this client – hlookup3criteria and multiHLookUp. The first two (2) are self-explanatory. The last, multiHLookUp, was developed because they wanted a multi-criteria hLookUp function that they could use if they had more than 3 criteria. I decided to give them a function that gave them the flexibility of accepting 2, 3, 4 or 5 criteria. If you are interested in using either of these functions you may write to me and I will send them to you.
I hope you found this post helpful and would love to hear from you if you use it in a project. If you do use it, I would just ask that you give me credit – Joe Valencia, The Happy Coder – and include my email address, joe@joevalencia.site90.com
If you would like to know more about this project or have one you would like to discuss, please write to me at joe@joevalencia.site90.com.
Filed under: Excel, Microsoft Office, Programming, VBA Tagged: custom function, data collection, Excel, HLOOKUP, Microsoft Excel, multi-criteria hLookUp, multi-criteria vLookUp, Proactive Customer Service, vba, VLOOKUP
