我在A2:D2列和F1上有一个样本数据(1,2,3,4),我有(1,3),在G1(2,3,4)上。
建议我使用此公式
=INDEX($A2:$D2,AGGREGATE(15,6,ROW($A2:$D2)/ISNUMBER(SEARCH(TEXT($A2:$D2,"000,"),F$1&",")),1))
应该分别给出F2和G2上的匹配值。由于两个值(1,3)匹配,因此结果应为F2上的(1,3)和G2上的(2,3,4)。
因此,基本上,该公式应该像索引/匹配公式一样工作,但它不是单个值,而是用于逗号分隔的2或3个值。
当我尝试使用公式时,会不断出现#NUM!
错误。请给我一点帮助,以使此公式生效?
谢谢。
注意:我欢迎其他解决方案,但如果需要VBA,则必须使用用户定义的公式。没有UDF,对我没有用。
答案 0 :(得分:1)
UDF应该看起来像这样
Option Explicit
Function MultFind(r As Range, s As String) As String
Dim c As Range
Dim result As String
For Each c In r
If InStr("," & s & ",", "," & c & ",") <> 0 Then result = result & c & ","
Next c
If Len(result) > 0 Then result = Left(result, Len(result) - 1)
MultFind = result
End Function
称为
=MultFind($A2:$D2,F1)
修改
此处带有定界符的可选参数:
Option Explicit
Function MultFind(r As Range, s As String, Optional delimiter As String = ",") As String
Dim c As Range
Dim result As String
' Check each cell in range against search string
For Each c In r
If InStr(delimiter & s & delimiter, delimiter & c & delimiter) <> 0 Then result = result & c & delimiter
Next c
' Delimiter may be more than one character
If Len(result) > 0 Then result = Left(result, Len(result) - Len(delimiter))
MultFind = result
End Function