提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何在Excel中的一個單元格中查看返回多個值?

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

使用用戶定義函數在一個單元格中查找返回多個值

Vlookup使用kutools for Excel在一個單元格中返回多個值

DOC-VLOOKUP-多值酮細胞1


Vlookup在一個單元中返回多個相應的值:

Kutools for Excel先進的組合行 utiltiy,您可以快速將多個重複行組合到一個基於鍵列的記錄中,並且還可以對其他列應用一些計算,例如sum,average,count等。

  • 1。 指定您想要組合其他列的鍵列;
  • 2。 為您的組合數據選擇分隔符。

doc根據條件10合併文本

Kutools for Excel:比200方便的Excel加載項,可以在60天免費試用。 下載並免費試用現在!

>

傳統的VLOOKUP函數可能無法幫助您解決此問題,但是,我將為您創建一個用戶定義函數來處理此任務。 請做如下:

1。 激活你想要查看多個值到一個單元格的工作表。

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

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

VBA代碼:Vlookup在一個單元格中返回多個值

Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
'Update 20150310
Dim rng As Range
Dim xResult As String
xResult = ""
For Each rng In pWorkRng
    If rng = pValue Then
        xResult = xResult & " " & rng.Offset(0, pIndex - 1)
    End If
Next
MYVLOOKUP = xResult
End Function

4。 然後保存並關閉此代碼,返回到工作表,並輸入此公式: = myvlookup(E6,$ A $ 2:$ C $ 15,2) 放入要放置結果的特定空白單元格中,然後按 輸入 鍵可以獲取所需的一個單元格中的所有對應值,請參閱截圖:

DOC-VLOOKUP-多值酮細胞2

注意:

在上面的公式中: E6 是您想要返回其相關信息的價值, A2:C15 是您使用的數據范圍,數字 2 表示您的匹配值被返回的列號。


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

Kutools for Excel : 與超過300方便的Excel加載項,在60天免費試用沒有限制.

安裝後 Kutools for Excel請按照以下步驟進行:

1。 根據其他數據選擇要合併一列數據的數據范圍。

2。 點擊 Kutools > 內容 > 先進的組合行,看截圖:

DOC-VLOOKUP-多值酮細胞5

3。 在裡面 根據列組合行 對話框中,單擊要合併的關鍵字列,然後單擊 首要的關鍵。 然後點擊您想要根據關鍵字列組合其數據的另一列,然後點擊 結合 要選擇一個分隔符來分隔組合數據,請參見截圖:

DOC-VLOOKUP-多值酮細胞6

4. 然後點擊 OK 按鈕,你會得到以下結果:

DOC-VLOOKUP-多值酮細胞3 -2 DOC-VLOOKUP-多值酮細胞4-4

點擊了解有關此高級合併行功能的更多信息。

立即下載並免費試用Kutools for Excel!


Kutools for Excel:比200方便的Excel加載項,可以在60天免費試用。 下載並免費試用現在!


相關文章:

如何在Excel中查找下一個最大值?

如何在Excel中的二維表中查找?

如何查看另一個工作簿?


Kutools for Excel - 最佳辦公生產力工具提高80%的生產力

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能60天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Martin · 14 days ago
    Unfortunately it does not work for me =myvlookup(E6,$A$2:$C$15,2) as the result is #NAME?
  • To post as a guest, your comment is unpublished.
    Steven · 1 months ago
    Thank you. I just modified so that it will replace the comma at the start and replace the last comma with "And".

    (Working with states)

    Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)

    Dim rng As Range
    Dim xResult, States As String
    Dim Pos As Long
    xResult = ""
    For Each rng In pWorkRng
    If rng = pValue Then
    xResult = xResult & ", " & rng.Offset(0, pIndex - 1)
    End If
    Next
    States = Right(xResult, Len(xResult) - 2)
    Pos = InStrRev(States, ",")

    If Pos > 1 Then
    States = Mid(States, 1, Pos - 1) & Replace(States, ",", " AND", Pos)

    End If

    MYVLOOKUP = States
    End Function
  • To post as a guest, your comment is unpublished.
    Devinder Kaur · 5 months ago
    do we have the code to work it from last to first. like we have multiple values in columnB and we want them in row with column A remain same.
  • To post as a guest, your comment is unpublished.
    Pam · 10 months ago
    For some reason this macro is extremely slow for me, every time I click anywhere it takes about 5-10 minutes to calculate.
  • To post as a guest, your comment is unpublished.
    Alex · 11 months ago
    Tthat's what I wanted! Thank you very much XD
  • To post as a guest, your comment is unpublished.
    Tom T · 1 years ago
    This is amazing, thank you!
  • To post as a guest, your comment is unpublished.
    Mark · 1 years ago
    Great function, however chunking through 100,000 records proves a bit much for my poor laptop, will need to let it run overnight!
  • To post as a guest, your comment is unpublished.
    Will · 1 years ago
    Is there a way of amending the result so instead of showing 1000 1000 -1000 it would show for example 1,000/1,000/(1,000) ?
  • To post as a guest, your comment is unpublished.
    Kabilankumar · 2 years ago
    Great macro, useful.

    But need to know can it be modified to check 2 criteria & does anyone found anyway to make wildcards work on it.

    Any help?
  • To post as a guest, your comment is unpublished.
    Jay · 2 years ago
    No matter what I do, I always get #value! returned instead of a result. vlookup works just fine, so the data works. Already followed the process of enabling macros. I even combined everything into a single sheet. Any ideas??
  • To post as a guest, your comment is unpublished.
    Curtis · 2 years ago
    This worked perfectly, but it did take me some time to get the Function work properly within my 20 tab, 50k+ line spreadsheet.
    Now the BIG question is how to take that delimited string and then use each entry as an Index/Match (not married to Index/Match, but it seems faster) lookup value in another dataset, returning the SUM value of all returns into one cell. My scenario is that I have a Single Order that has multiple invoices. Your MYVLOOKUP Function works superbly to report back all of the invoices in one cell. What I want to do now is to take each concatenated return with the reported cell, spin through that array and total the amounts of payment of each invoice back into the formula cell.

    I appreciate any help that you can offer on this and thanks for the MYVLOOKUP Function!
  • To post as a guest, your comment is unpublished.
    Alex Chow · 2 years ago
    To get unique record, you may use below: (modified by refer other user code)


    Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
    'Update 20150310
    'Updated 6/9/16 Jay Coltrain
    'Dim rng As Range
    Dim xResult As String
    xResult = ""

    Dim Rows As Long, i As Long
    Rows = pWorkRng.Rows.Count

    For i = 1 To Rows
    If pWorkRng.Cells(i, 1).Value = pValue Then
    xResult = xResult & "," & pWorkRng.Cells(i, 1).Offset(0, pIndex - 1)
    End If
    Next i

    Dim varSection As Variant
    Dim sTemp As String
    Dim sDelimiter As String

    sDelimiter = ","

    For Each varSection In Split(xResult, sDelimiter)
    If InStr(1, sDelimiter & sTemp & sDelimiter, sDelimiter & varSection & sDelimiter, vbTextCompare) = 0 Then
    sTemp = sTemp & sDelimiter & varSection
    End If
    Next varSection


    MYVLOOKUP = Mid(sTemp, Len(sDelimiter) + 1)
    End Function
  • To post as a guest, your comment is unpublished.
    Spyros Tzortzis · 2 years ago
    Heads-up. I figured out how to get any separator in that output. Its rudimentary. But I figured it out.

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

    The last and most wished for thing though is enabling it to work with wildcards in the search criteria. Thank you again for this beautiful and brilliant solution. Extremely helpful. Now just want to get get the macro to run and be installed in my excel perminantly no matter what I'm doing so I can use it when I need to.

    And wildcards!

    Thank you so much.

    Wildcards are all that are left to do.
  • To post as a guest, your comment is unpublished.
    Spyros Tzortzis · 2 years ago
    Hi,

    It works well. What I would like to do is adapt the code to seperate thee value results with "///" or any other marker (for technical reasons, I don't want just a single character seperator).

    Also, I noticed that this formula doesnt work with a wild card. I know i am asking too much, but it doesnt as vlookup can work when i search for =myvlookup("*"&E6&"*",$A$2:$C$15,2) which it would do/could do.

    Any assistance?
  • To post as a guest, your comment is unpublished.
    Ajay · 2 years ago
    Return nothing! after applying MYLOOKUP giving no result but blank.
  • To post as a guest, your comment is unpublished.
    Rob · 3 years ago
    Notify me of follow-up comments
  • To post as a guest, your comment is unpublished.
    cory · 3 years ago
    This works great, but I still need help with the command function to remove duplicates from the results.
  • To post as a guest, your comment is unpublished.
    cory · 3 years ago
    This works great, but I need help with the command to remove duplicates from the results. Seriously though, Great work.
  • To post as a guest, your comment is unpublished.
    Jay · 3 years ago
    This is exactly what I was looking for and did not think of just making my own UDF. However it will not function exactly like VLOOKUP. If the string you are looking for is not only in the first column then it could give you data outside the original range passed.

    Name Number Other name Column not in range passed
    Jay 1 Jay 1
    Jay 2 Jay 2
    Chris 3 Chris 3
    Jorge 4 Jorge 4
    Jay 5 Jay 5
    Jorge 6 Jorge 6

    If the above table were cells A1:D7 if you passed only A1:C7 your "MYVLOOKUP" function returns 1 1 2 2 5 5 when you would expect it to return 1 2 5.

    The changes below fix the issue:

    Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
    'Update 20150310
    'Updated 6/9/16 Jay Coltrain
    'Dim rng As Range
    Dim xResult As String
    xResult = ""

    Dim Rows As Long, i As Long
    Rows = pWorkRng.Rows.Count

    For i = 1 To Rows
    If pWorkRng.Cells(i, 1).Value = pValue Then
    xResult = xResult & " " & pWorkRng.Cells(i, 1).Offset(0, pIndex - 1)
    End If
    Next i

    'For Each rng In pWorkRng
    ' If rng = pValue Then
    ' xResult = xResult & " " & rng.Offset(0, pIndex - 1)
    ' End If
    'Next
    MYVLOOKUP = xResult
    End Function
  • To post as a guest, your comment is unpublished.
    Ari · 3 years ago
    Thank you for the VBA-code. I got exactly what I want!

    I modified only the code " rng.Offset(0, pIndex - 1) " to " rng.Offset(0, pIndex - 2) " . So is MYVLOOKUP able to search from Right to Left.
  • To post as a guest, your comment is unpublished.
    Ankit Jain · 3 years ago
    Hi,
    i am really impressed with the work and its so easy to create one to use this function. however i need further support.

    My ? is that how can i select a number from a cell with multiple cell in my vlookup array. i.e.

    If cell A1 = 100, A2 = 350, A3 = 69

    C1 = 100; 1222; 12133
    C2 = 69; 222
    D1 = Apple
    D2 = banana

    So how can select 100 from my table array column C to derive correspondent D1 = apple

    Please note that i have 7 digit numbers in my lookup value and table array which is separated by a ";". I would really appreciate if you can solve this and help me in saving a lot time.
  • To post as a guest, your comment is unpublished.
    Ajay · 3 years ago
    Awesome work.. Got exactly what I want !!! Love it !!
  • To post as a guest, your comment is unpublished.
    Colleen · 3 years ago
    Thank you for sharing the above code. I've been using this for several months now but today it doesn't seem to work. I'm getting blank cells instead of the usual error when there is data to be returned. Any thoughts?
  • To post as a guest, your comment is unpublished.
    David V · 4 years ago
    Thanks for the VBA code above. Can you tell me how to make the results enter onto a new line in the cell, ie like Alt-Enter
    300
    400
    1000
    1300
  • To post as a guest, your comment is unpublished.
    David Atwell · 4 years ago
    How would I adjust this formula to separate each returned value but ", " as well as only return unique values?