數據:Excel的VBA塔B爲單細胞(昏迷分開),在Excel中基於列A
1 Mary
1 John
1 Sam
2 Alaina
2 Edward
結果我預期是:
1 Mary, John, Sam
2 Alaina, Edward
數據:Excel的VBA塔B爲單細胞(昏迷分開),在Excel中基於列A
1 Mary
1 John
1 Sam
2 Alaina
2 Edward
結果我預期是:
1 Mary, John, Sam
2 Alaina, Edward
用戶allquixotic回答非常相似的題。源here
這是他的VBA的方法:
Option Explicit
Function allquixotic(param As Variant, search As Range, values As Range, Optional absolute As Boolean = False) As String
Dim sep As String, retval As String
Dim i As Integer, rownum As Integer
Dim look As Range, j As Range
sep = ", "
retval = ""
For i = 1 To search.Rows.Count
Set look = search.Cells(i, 1)
If absolute Then
rownum = look.Row
Else
rownum = i
End If
If look.Value = param Then
If absolute Then
Set j = values.Worksheet.Cells(rownum, values.Column)
Else
Set j = values.Cells(i, 1)
End If
retval = IIf(retval = "", retval & j.Value, retval & sep & j.Value)
End If
Next
allquixotic = retval
End Function
Use the worksheet function (feel free to rename it) by using a formula like
=allquixotic(A1,$A$1:$A$15,$B$1:$B$15,true)
Use the fill handle to put the formula in all the cells
的參數如下:
=allquixotic(look_cell, key_range, value_range, absolute)
look_cell:
的第一個參數,應該是一個單細胞或值字面值。有效的輸入包括3,$ 6.25,「Hello」等等。這是您在key_range中試圖找到的值。
key_range:
這應該是一個範圍細胞(多於一個小區)的;如果absolute
是真的,那麼你會得到非常奇怪的結果,除非這是一個連續的範圍(所有的值都是連續的行)。
value_range:
這應該是一個範圍細胞(多於一個小區)的;如果absolute
是真的,那麼你會得到非常奇怪的結果,除非這是一個連續的範圍(所有的值都是連續的行)。
absolute:
如果屬實,那麼我們將使用(相對於整個電子表格的行數)在key_range
每個「發現」行絕對行號,以確定哪些行從value_range
上提取值。如果錯誤,我們將使用相對數字;例如,如果我們在第三個行key_range
中找到一個匹配項,那麼我們將從第三個行提取值value_range
。推薦值爲FALSE,或者您可以省略默認值。
注意:此功能不支持,其中鍵和值範圍在列的情況下,但它應該是很容易使其適應這一點。
另外,如果您在key_range
或value_range
中指定了多個列,則只會使用最左邊的列。
再次,所有功勞都去allquixotic。