跳到主要內容

如何在Excel中的一個單元格中通過vlookup返回多個值?

通常,在Excel中,當您使用VLOOKUP函數時,如果存在多個與條件匹配的值,則只需獲取第一個即可。 但是,有時,您希望將符合條件的所有相應值返回到一個單元格中,如下面的屏幕截圖所示,如何解決呢?

Vlookup使用TEXTJOIN函數將多個值返回到一個單元格中(Excel 2019和Office 365)

Vlookup使用用戶定義的函數將多個值返回到一個單元格

Vlookup通過有用的功能將多個值返回到一個單元格


Vlookup使用TEXTJOIN函數將多個值返回到一個單元格中(Excel 2019和Office 365)

如果您擁有較高版本的Excel(例如Excel 2019和Office 365),則有一個新功能- 文字加入,借助這一強大的功能,您可以快速進行vlookup並將所有匹配的值返回到一個單元格中。

Vlookup將所有匹配的值返回到一個單元格

請將以下公式應用於要放入結果的空白單元格,然後按 Ctrl + Shift + Enter 鍵一起獲得第一個結果,然後將填充手柄向下拖動到要使用此公式的單元格,您將獲得所有對應的值,如下圖所示:

=TEXTJOIN(",",TRUE,IF($A$2:$A$11=E2,$C$2:$C$11,""))

注意: 在上式中 A2:A11 查找範圍包含查找數據, E2 是查找值, C2:C11 是您要從中返回匹配值的數據范圍,“,“是用於分隔多個記錄的分隔符。

Vlookup將所有沒有重複的匹配值返回到一個單元格

如果要基於查找數據返回所有匹配值而不重複,則以下公式可能會對您有所幫助。

請複制以下公式並將其粘貼到空白單元格中,然後按 Ctrl + Shift + Enter 鍵在一起以獲得第一個結果,然後復制此公式以填充其他單元格,您將獲得所有對應的值,而沒有重複的值,如下面的屏幕截圖所示:

=TEXTJOIN(",", TRUE, IF(IFERROR(MATCH($C$2:$C$11, IF(E2=$A$2:$A$11, $C$2:$C$11, ""), 0),"")=MATCH(ROW($C$2:$C$11), ROW($C$2:$C$11)), $C$2:$C$11, ""))

注意: 在上式中 A2:A11 查找範圍包含查找數據, E2 是查找值, C2:C11 是您要從中返回匹配值的數據范圍,“,“是用於分隔多個記錄的分隔符。

Vlookup使用用戶定義的函數將多個值返回到一個單元格

上面的TEXTJOIN函數僅適用於Excel 2019和Office 365,如果您具有其他較低的Excel版本,則應使用一些代碼來完成此任務。

Vlookup將所有匹配的值返回到一個單元格

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:Vlookup將多個值返回到一個單元格

Function ConcatenateIf(CriteriaRange As Range, Condition As Variant, ConcatenateRange As Range, Optional Separator As String = ",") As Variant
'Updateby Extendoffice
Dim xResult As String
On Error Resume Next
If CriteriaRange.Count <> ConcatenateRange.Count Then
    ConcatenateIf = CVErr(xlErrRef)
    Exit Function
End If
For i = 1 To CriteriaRange.Count
    If CriteriaRange.Cells(i).Value = Condition Then
        xResult = xResult & Separator & ConcatenateRange.Cells(i).Value
    End If
Next i
If xResult <> "" Then
    xResult = VBA.Mid(xResult, VBA.Len(Separator) + 1)
End If
ConcatenateIf = xResult
Exit Function
End Function

3。 然後保存並關閉此代碼,返回到工作表,然後輸入以下公式: =CONCATENATEIF($A$2:$A$11, E2, $C$2:$C$11, ", ") 放入要放置結果的特定空白單元格中,然後向下拖動填充手柄以在所需的一個單元格中獲取所有對應的值,請參見屏幕截圖:

注意: 在上式中 A2:A11 查找範圍包含查找數據, E2 是查找值, C2:C11 是您要從中返回匹配值的數據范圍,“,“是用於分隔多個記錄的分隔符。

Vlookup將所有沒有重複的匹配值返回到一個單元格

要忽略返回的匹配值中的重複項,請使用以下代碼。

1。 按住 Alt + F11鍵 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:Vlookup並將多個唯一匹配的值返回到一個單元格中

Function MultipleLookupNoRept(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer)
'Updateby Extendoffice
    Dim xDic As New Dictionary
    Dim xRows As Long
    Dim xStr As String
    Dim i As Long
    On Error Resume Next
    xRows = LookupRange.Rows.Count
    For i = 1 To xRows
        If LookupRange.Columns(1).Cells(i).Value = Lookupvalue Then
            xDic.Add LookupRange.Columns(ColumnNumber).Cells(i).Value, ""
        End If
    Next
    xStr = ""
    MultipleLookupNoRept = xStr
    If xDic.Count > 0 Then
        For i = 0 To xDic.Count - 1
            xStr = xStr & xDic.Keys(i) & ","
        Next
        MultipleLookupNoRept = Left(xStr, Len(xStr) - 1)
    End If
End Function

3。 插入代碼後,然後單擊 工具 > 參考 在打開 Microsoft Visual Basic for Applications 窗口,然後在彈出 參考– VBAProject 對話框,檢查 Microsoft腳本運行時 在選項 可用參考 列錶框,請參見屏幕截圖:

4。 然後點擊 OK 關閉對話框,保存並關閉代碼窗口,返回到工作表,然後輸入以下公式: =MultipleLookupNoRept(E2,$A$2:$C$11,3) into a blank cell where you want to output the result, and then drag the fill hanlde down to get all matching values, see screenshot:

注意: 在上式中 A2:C11 是您要使用的數據范圍, E2 是查找值,數字 3 是包含返回值的列號。

Vlookup通過有用的功能將多個值返回到一個單元格

 如果您有我們的 Excel的Kutools,其 高級合併行 功能,您可以根據相同的值快速合併或合併行,並根據需要進行一些計算。

注意:要應用此 高級合併行,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請執行以下操作:

1。 選擇要根據另一列合併一個列數據的數據范圍。

2。 點擊 庫工具 > 合併與拆分 > 高級合併行,請參見屏幕截圖:

3。 在彈出 高級合併行 對話框:

  • 單擊要基於其組合的鍵列名稱,然後單擊 首要的關鍵
  • 然後單擊要基於鍵列合併其數據的另一列,然後單擊 結合 選擇一個分隔符以分隔組合數據。

4. 然後點擊 OK 按鈕,您將獲得以下結果:

立即下載和免費試用Excel的Kutools!


更多相關文章:

  • VLOOKUP函數以及一些基本和高級示例
  • 在Excel中,VLOOKUP函數對於大多數Excel用戶而言是一項功能強大的函數,用於在數據范圍的最左側查找值,並在您指定的列的同一行中返回匹配值。 本教程通過Excel中的一些基本示例和高級示例討論如何使用VLOOKUP函數。
  • 根據一個或多個條件返回多個匹配值
  • 通常,使用VLOOKUP函數對我們大多數人來說查找特定值並返回匹配項很容易。 但是,您是否曾經嘗試過根據一個或多個條件返回多個匹配值? 在本文中,我將介紹一些用於解決Excel中復雜任務的公式。
  • Vlookup並垂直返回多個值
  • 通常,您可以使用Vlookup函數來獲取第一個對應的值,但是有時,您希望基於特定條件返回所有匹配的記錄。 在本文中,我將討論如何進行vlookup並將所有匹配值垂直,水平或返回到單個單元格中。
  • Vlookup並從下拉列表中返回多個值
  • 在Excel中,如何從下拉列表中進行vlookup並返回多個相應的值,這意味著當您從下拉列表中選擇一項時,它的所有相對值會立即顯示。 本文,我將逐步介紹解決方案。

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (43)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have created a problem.
"I" have combined a "Textjoin" end "Vlookup" to return multiple values in to one single cell.
My problem is that the formula have to have an exact value to look for and I want it to lookup an "almost" match or Partial match.

Example: I have made a schedule how we ate going to work and a D1 is working from 07:30-16:00. And to lookup D1 is not the problem, the problem is that my boss sometimes puts other stuff togeather with the D1... Like "D1 +" or "D1 meeting".
Since my formula only lookup "D1" it misses for example the "D1 +".

My formula (that I have gotten from the web) =TEXTJOIN(" och ";SANT;OM($B$3:$B$15=$C$22:$F$22;$A$3:$A$15;""))It´s in swedish so "SANT" is "TRUE" and "OM" is "IF".

How can I make the formula lookup all the cells that have some form of "D1" in it and return all those to the same cell?
No matter if it says "D1 +" or "D1 meeting" or whatever.
The reson I want this, is because the boss always leave "D1" but can add other text behind the "D1"... and just because of that, my boss messes up my formula.
This comment was minimized by the moderator on the site
Hi!
This is a great VBA-Code which could help me a lot.But when I start the Function MultipleLookupNoRept Excel crashs...I´ve got a Dataset with about 6.000 Rows (Excel 2013).... is this too much for the VBA Function?

Thanks!
This comment was minimized by the moderator on the site
Hello Mr.XXL,Sorry to hear that. The row limit for Excel 2013 is 1048576. Therefore, maybe the VBA code is the reason for the crash.
Anyway, I would love to offer you another VBA code for Vlookup To Return All Matching Values Without Duplicates Into One Cell. Please use the VBA code below:
Option Explicit

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)

Dim i As Long
Dim temp() As Variant
Dim result As String
ReDim temp(0)

For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
temp(UBound(temp)) = Return_val_col.Cells(i, 1).Value
ReDim Preserve temp(UBound(temp) + 1)
End If
Next

If temp(0) <> "" Then
ReDim Preserve temp(UBound(temp) - 1)
Unique temp
For i = LBound(temp) To UBound(temp)
result = result & " " & temp(i)
Next i
Lookup_concat = Trim(result)
Else
Lookup_concat = ""
End If

End Function

Function Unique(tempArray As Variant)

Dim coll As New Collection
Dim Value As Variant

On Error Resume Next
For Each Value In tempArray
If Len(Value) > 0 Then coll.Add Value, CStr(Value)
Next Value
On Error GoTo 0

ReDim tempArray(0)

For Each Value In coll
tempArray(UBound(tempArray)) = Value
ReDim Preserve tempArray(UBound(tempArray) + 1)
Next Value

End Function

After you insert this VBA code in the Module, please type the formula =Lookup_concat(E2,$A$2:$A$14,$C$2:$C$14) into a blank cell where you want to output the result, and then drag the fill hanlde down to get all matching values. Please see the file I uploaded in this comment. Hope it solves your problem. 
Sincerely,Mandy

This comment was minimized by the moderator on the site
Hi, Thanks so much this worked!I used it to pull dates, that populated in the serial number format (<span style="letter-spacing: 0.2px; color: inherit; font-family: inherit; font-style: inherit; font-variant-ligatures: inherit; font-variant-caps: inherit; font-weight: inherit;">Changing the format to short date format using =TEXT(A2,”mm/dd/yy”) OR =DATEVALUE(A2) are not working. Do you have any solutions?</span>
This comment was minimized by the moderator on the site
Thank you for the explanations, however the function 'MultipleLookupNoRept' does not work on my file, could you tell me if an error exists.
This comment was minimized by the moderator on the site
Hi, Hasnae,Please check if you miss the third step -  check Microsoft Scripting Runtime option in the Available References list box.

This comment was minimized by the moderator on the site
Thank you so much for the code. Is there a way I can use the code to look up multiple values from multiple sheets? I tried to combine your function with IFERROR function but it doesn't seem to work.
This comment was minimized by the moderator on the site
Can this be modified to place the sum of those values? Instead of (400 400 400 400 400 400), can it sum them to show (2400)?
This comment was minimized by the moderator on the site
How with HLookUp function?
This comment was minimized by the moderator on the site
thanks for the code. I have modified it to allow you to optionally specify your own separator, Default is " ", if you specify the separator as"#cr" it will insert a CR/LF so the values will be on a separate line in the cell. It only applies the separator if there are multiple values

Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long, Optional ByVal pSep As Variant)

' ### Returns multiple values from a table into 1 cell ###

' pValue is the key value to lookup

' WorkRng is the Table you want to look up

' pIndex is the column # for the values to be returned from the pWorkRng

' pSep (optional) is the separator to be used. if omitted then default is a space (it doesn't apply the separator for the 1st entry)

' if the separtor = "#cr" it will separate the values on different line in the cell

Dim rng As Range

Dim sSep As String

Dim xResult As String

Dim Item1 As Boolean

Item1 = True



If IsMissing(pSep) = True Then

sSep = vbCr

Else

If pSep = "#cr" Then

sSep = vbCrLf

Else

sSep = pSep

End If

End If



xResult = ""

For Each rng In pWorkRng

If rng = pValue Then

If Item1 Then

xResult = xResult & rng.Offset(0, pIndex - 1)

Item1 = False

Else

xResult = xResult & sSep & rng.Offset(0, pIndex - 1)

End If

End If

Next

MYVLOOKUP = xResult

End Function
This comment was minimized by the moderator on the site
Thank you for this, the line breaks are what i needed to top this formula off! Question, is there a way to modify the code so that two values are compared? For example, similar to what we see with index and match, can i look for Product and Quantity columns, and based on those parameters it outputs results from the Region Column?
This comment was minimized by the moderator on the site
Thanks a lot for this code, it is very helpful. Does anyone know away to sum the values in the cell to just have at total of them.
Cheers
This comment was minimized by the moderator on the site
Hello, James, to sum values based on the corresponding items, the following article may help you, please chek it:
https://www.extendoffice.com/documents/excel/1268-excel-combine-duplicate-rows-and-sum.html
This comment was minimized by the moderator on the site
I have a server, it has connected with multiple applications. I want to compare compare two column and get the related applications details for that server.

What is the command for that.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations