图片显示了我拥有的数据集。 ICTO_ID
行中的H
重复多次,并且具有多个乘积,例如,ICTO-247
具有Fx
和eFX
作为{{1 }}(col Product
)。现在在第J
行(产品)中,我需要显示E
具有ICTO-247
,FX
作为eFX
。因此,单元格Product
应该读为E3
,FX
。
现在,如果我执行vlookup,则返回的唯一值将是eFX
,因为这是基于FX
的第一个匹配项。我需要一种能够返回两个值的方法。这可以是代码或公式的形式。
任何帮助将不胜感激。
非常感谢
答案 0 :(得分:2)
您可以在VBA函数中使用Dictionary
对象
Function GetIctoProducts(ictoID As String) As Variant
Dim cel As Range
GetIctoProducts = CVErr(xlErrNA)
With CreateObject("Scripting.dictionary")
For Each cel In Range("H2", Cells(Rows.Count, "H").End(xlUp))
.Item(cel.Value) = .Item(cel.Value) & cel.Offset(, 2).Value & ", "
Next
If .Exists(ictoID) Then GetIctoProducts = Left(.Item(ictoID), Len(.Item(ictoID)) - 2)
End With
End Function
答案 1 :(得分:0)
不包含当我们想要获取字符串值的单元格中有错误时的情况。其他都应该没问题。
在这种情况下,您应该将函数放入标准模块中,并在Excel中针对此确切情况,在单元格 E2 中使用以下公式(并向下复制):
=TLU(C2,H$2:H$5,1)
还有其他功能,例如列可以在任何位置(左,右,中间),可以更改分隔符(默认情况下,第4个参数“,”)和区分大小写(第5个参数)。玩吧。
Option Explicit
Function TLU(SourceValue As Variant, SourceColumn As Range, _
ValueColumnOffset As Long, Optional Separator As String = ", ", _
Optional CaseInSensitive As Long = 1) As String
Dim ValueColumn As Range ' Value Column
Dim vntS As Variant ' Source Array
Dim vntV As Variant ' Value Array
Dim colSource As Long ' SourceColumn Column Number
Dim colTC As Long ' ThisCell Column Number
Dim i As Long ' Source and Value Array Counter
Dim strS As String ' Source String
Dim strR As String ' Resulting String
' Calculate SourceColumn Number.
colSource = SourceColumn.Column
' Calculate Value Column and eliminate some impossible scenarios
' using error handling.
On Error GoTo ProcedureExit
Set ValueColumn = Cells(SourceColumn.Row, SourceColumn.Column) _
.Offset(, ValueColumnOffset).Resize(SourceColumn.Rows.Count)
On Error GoTo 0
' Calculate ThisCell Column Number
colTC = Application.ThisCell.Column
' Check ThisCell Column Number against SourceColumn Number and
' Value Column Number.
If colTC = colSource Or colTC = ValueColumn.Column Then Exit Function
' Write SourceValue to Source String and values of ranges to arrays.
strS = CStr(SourceValue): vntS = SourceColumn: vntV = ValueColumn
' Loop through arrays and build Resulting String.
For i = 1 To UBound(vntS)
If StrComp(CStr(vntS(i, 1)), strS, CaseInSensitive) = 0 Then
If strR <> "" Then
strR = strR & Separator & vntV(i, 1)
Else
strR = vntV(i, 1) ' First value has no separator.
End If
End If
Next
TLU = strR
ProcedureExit:
End Function
答案 2 :(得分:0)
用于Office 365订阅者的非vba解决方案。
=TEXTJOIN(", ",,FILTER($J$2:$J$10,$H$2:$H$10=$C2))