基本上我希望比较2个单元格的内容,然后用差异填充新单元格。我想要比较其内容的2个单元格都是包含产品名称的列表。一个例子是:
单元格1包含A,b,c,d
单元格2包含b,c
我希望单元格3填充A和D
我基本上希望与vlookup功能相反,但不知道我会怎么做。
提前感谢您的帮助。
答案 0 :(得分:0)
这是您可以使用的UDF:
' Returns a `delimiter`-joined list containing
' items from minuend (a `delimiter`-joined list)
' but not items from subtrahend (a `delimiter`-joined list)
Public Function SET_SUB(minuend As String, subtrahend As String, Optional delimiter As Variant)
If IsMissing(delimiter) Then delimiter = "," ' Set default delimiter as comma
Dim i As Integer
Dim emptyList As Boolean: emptyList = True
' Retrieve list items
Dim fullSet As Variant
Dim removeSet As Variant
fullSet = Split(minuend, delimiter)
removeSet = Split(subtrahend, delimiter)
SET_SUB = ""
' Loop through subtrahend, removing matches
For i = 0 To UBound(fullSet)
If IsError(Application.Match(fullSet(i), removeSet, 0)) Then
SET_SUB = SET_SUB & fullSet(i) & delimiter
emptyList = False
End If
Next
' Remove last delimiter for non-empty list
If Not emptyList Then
SET_SUB = Left(SET_SUB, Len(SET_SUB) - Len(delimiter))
End If
End Function
将其删除到模块中,您可以在工作表中访问该功能(如果您不熟悉,请参阅UDF here的信息)。
它获取第一个列表中的项目,删除第二个列表中的项目,并返回设置的差异。您可以选择添加"分隔符"如果您希望列表以逗号以外的其他内容分隔,则参数。
建立榜样:
A1 = a,b,c,d
A2 = b,c
A3 = =SET_SUB(A1, A2)
= a,d
对于以分号分隔的列表:
A1 = a; b; c; d
A2 = b; c
A3 = =SET_SUB(A1, A2, ";")
= a; d