You are reading the article Get Multiple Lookup Values In A Single Cell (With &Amp; Without Repetition) updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Get Multiple Lookup Values In A Single Cell (With &Amp; Without Repetition)
Can we look-up and return multiple values in one cell in Excel (separated by comma or space)?
I have been asked this question multiple times by many of my colleagues and readers.
Excel has some amazing lookup formulas, such as VLOOKUP, INDEX/MATCH (and now XLOOKUP), but none of these offer a way to return multiple matching values. All of these work by identifying the first match and return that.
So I did a bit of VBA coding to come up with a custom function (also called a User Defined Function) in Excel.
Update: After Excel released dynamic arrays and awesome functions such as UNIQUE and TEXTJOIN, it’s now possible to use a simple formula and return all the matching values in one cell (covered in this tutorial).
In this tutorial, I will show you how to do this (if you’re using the latest version of Excel – Microsoft 365 with all the new functions), as well as a way to do this in case you’re using older versions (using VBA).
So let’s get started!
If you’re using Excel 2023 or prior versions, go to the next section where I show how to do this using VBA.
With Microsoft 365 subscription, your Excel now has a lot more powerful functions and features that are not there in prior versions (such as XLOOKUP, Dynamic Arrays, UNIQUE/FILTER functions, etc.)
So if you’re using Microsoft 365 (earlier known as Office 365), you can use the methods covered in this section could look up and return multiple values in one single cell in Excel.
And as you will see, it’s a really simple formula.
Below I have a data set where I have the names of the people in column A and the training that they have taken in column B.
For each person, I want to find out what training they have completed. In column D, I have the list of unique names (from column A), and I want to quickly lookup and extract all the training that every person has done and get these in a single set (separated by a comma).
Below is the formula that will do this:=TEXTJOIN(", ",TRUE,IF(D2=$A$2:$A$20,$B$2:$B$20,""))
After entering the formula in cell E2, copy it for all the cells where you want the results.
How does this formula work?
Let me deconstruct this formula and explain each part in how it comes together gives us the result.
The logical test in the IF formula (D2=$A$2:$A$20) checks whether the name cell D2 is the same as that in range A2:A20.
It goes through each cell in the range A2:A20, and checks whether the name is the same in cell D2 or not. if it’s the same name, it returns TRUE, else it returns FALSE.
So this part of the formula will give you an array as shown below:
Since we only want to get the training for Bob (the value in cell D2), we need to get all the corresponding training for the cells that are returning TRUE in the above array.
This is easily done by specifying [value_if_true] part of the IF formula as the range that has the training. This makes sure that if the name in cell D2 matches the name in the range A2:A20, the IF formula would return all the training that person has taken.
And wherever the array returns a FALSE, we have specified the [value_if_false] value as “” (blank), so it returns a blank.
The IF part of the formula returns the array as shown below:
Where it has the names of the training Bob has taken and blanks wherever the name was not Bob.
Now, all we need to do is combine these training name (separated by a comma) and return it in one cell.
And that can easily be done using the new TEXTJOIN formula (available in Excel 2023 and Excel in Microsoft 365)
The TEXTJOIN formula takes three arguments:
the Delimiter – which is “, ” in our example, as I want the training to separated by a comma and a space character
TRUE – which tells the TEXTJOIN formula to ignore empty cells and only combine ones that are not empty
The If formula that returns the text that needs to be combined
If you’re using Excel in Microsoft 365 that already has dynamic arrays, you can just enter the above formula and hit enter. And if you’re using Excel 2023, you need to enter the formula, and hold the Control and the Shift key and then press Enter
Since the UNIQUE formula is only available fro Excel in Microsoft 365, you won’t be able to use this method in Excel 2023
In case there are repetitions in your data set, as shown below, you need to change the formula a little bit so that you only get a list of unique values in a single cell.
In the above data set, some people have taken training multiple times. For example, Bob and Stan have taken the Excel training twice, and Betty has taken MS Word training twice. But in our result, we do not want to have a training name repeat.
You can use the below formula to do this:=TEXTJOIN(", ",TRUE,UNIQUE(IF(D2=$A$2:$A$20,$B$2:$B$20,"")))
The above formula works the same way, with a minor change. we have used the IF formula within the UNIQUE function so that in case there are repetitions in the if formula result, the UNIQUE function would remove it.
If you’re using Excel 2023 or prior versions, then you will not have access to the TEXTJOIN formula. So the best way to then look up and get multiple matching values in a single cell is by using a custom formula that you can create using VBA.
To get multiple lookup values in a single cell, we need to create a function in VBA (similar to the VLOOKUP function) that checks each cell in a column and if the lookup value is found, adds it to the result.
Here is the VBA code that can do this:
Function SingleCellExtract(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer) Dim i As Long Dim Result As String For i = 1 To LookupRange.Columns(1).Cells.Count If LookupRange.Cells(i, 1) = Lookupvalue Then Result = Result & ” ” & LookupRange.Cells(i, ColumnNumber) & “,” End If Next i SingleCellExtract = Left(Result, Len(Result) – 1) End Function
Where to Put this Code?
In the module window (that will appear on the right), copy and paste the above code.
Now you are all set. Go to any cell in the workbook and type =SingleCellExtract and plug in the required input arguments (i.e., LookupValue, LookupRange, ColumnNumber).
How does this formula work?
This function works similarly to the VLOOKUP function.
It takes 3 arguments as inputs:
3. ColumnNumber – It is the column number of the table/array from which the matching value is to be returned (2 in this case).
When you use this formula, it checks each cell in the leftmost column in the lookup range and when it finds a match, it adds to the result in the cell in which you have used the formula.
Remember: Save the workbook as a macro-enabled workbook (.xlsm or .xls) to reuse this formula again. Also, this function would be available only in this workbook and not in all workbooks.
There is a possibility that you may have repetitions in the data.
If you use the code used above, it will give you repetitions in the result as well.
If you want to get the result where there are no repetitions, you need to modify the code a bit.
Here is the VBA code that will give you multiple lookup values in a single cell without any repetitions.
Function MultipleLookupNoRept(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer) Dim i As Long Dim Result As String For i = 1 To LookupRange.Columns(1).Cells.Count If LookupRange.Cells(i, 1) = Lookupvalue Then For J = 1 To i – 1 If LookupRange.Cells(J, 1) = Lookupvalue Then If LookupRange.Cells(J, ColumnNumber) = LookupRange.Cells(i, ColumnNumber) Then GoTo Skip End If End If Next J Result = Result & ” ” & LookupRange.Cells(i, ColumnNumber) & “,” Skip: End If Next i MultipleLookupNoRept = Left(Result, Len(Result) – 1) End Function
Once you have placed this code in the VB Editor (as shown above in the tutorial), you will be able to use the MultipleLookupNoRept function.
Here is a snapshot of the result you will get with this MultipleLookupNoRept function.
In this tutorial, I covered how to use formulas and VBA in Excel to find and return multiple lookup values in one cell in Excel.
While it can easily be done with a simple formula if you’re using Excel in Microsoft 365 subscription, if you’re using prior versions and don’t have access to functions such as TEXTJOIN, you can still do this using VBA by creating your own custom function.
You May Also Like the Following Excel Tutorials:
You're reading Get Multiple Lookup Values In A Single Cell (With &Amp; Without Repetition)
Update the detailed information about Get Multiple Lookup Values In A Single Cell (With &Amp; Without Repetition) on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!