以下代码公开了 VBA 的 Split 函数以供工作表使用 - 它返回已使用指定分隔符拆分的项目的行数组。例如,如果单元格 A1 包含文本“Item 1,Item 2”),则 EXPLODE(A1,",") 将返回包含元素“Item 1”和“Item 2”的数组。
Function EXPLODE(str As String, Optional delimiter As Variant) As Variant
If IsMissing(delimiter) Then
delimiter = " "
End If
EXPLODE = Split(str, delimiter)
End Function
它是一个数组函数。要在电子表格中使用返回的元素:
- 选择要在其中显示“分解”项目的单元格
- 输入函数,指定带有源字符串的单元格(或对包含源的单元格的引用)以及将在其上完成拆分的分隔符
- Complete the entry using the Control-Shift-Enter key combination.
或者,可以使用 INDEX 函数选择各个元素 -=INDEX(EXPLODE(A1,1,2)
使用前面的示例将返回“Item 2”。 (给定范围或数组,INDEX 函数返回第 i 行和第 j 列中的值。)此用法不需要将公式作为数组公式输入。
对于您的用例,与其他功能的组合是合适的。您有一个包含“aa、bb、cc”形式的多个项目(VLOOKUP 的结果)的字符串,并且想要确定是否可以在 A 列的任何单元格中找到该字符串的任何元素作为单独的项目.您需要一个函数,如果找到所有元素,则返回 True,否则返回 False。以下公式可实现该结果:
=SUM(SIGN(IFERROR(MATCH(TRIM(EXPLODE(D1,",")),$A:$A,0),0)))=COUNTA(EXPLODE(D1,","))
It is an array formula and needs to be entered with Control-Shift-Enter. Note that I used the contents of cell D1 in lieu of your lookup value. The TRIM function strips out any extraneous spaces between the elements of the string with multiple items.