2016-08-18 54 views
0

本质上来说,我期望比较2个单元格的内容,然后用不同的方法填充一个新单元格。我期待比较的2个单元格的内容都是包含产品名称的列表。一个例子是:比较2个单元格的内容,然后将任何差异复制到一个新单元格中

小区1含有A,B,C,d
细胞2含有B,C

我想细胞3〜然后用A和d

填充

我本质希望做一个vlookup函数的反面,但不知道我会怎么做。

在此先感谢您的帮助。

+0

你能更好地定义你的问题(我不确定你如何在你的例子中得到“A和B”作为期望的返回值)?你还可以向我们展示你迄今为止的尝试,以及为什么你的尝试失败了? – Mikegrann

+0

哎呀,这是一个错字。我纠正了它。 A和D是我想要的返回值,因为A&D仅位于单元格1中,而不是单元格2.我想我需要使用某种反向匹配函数,但仍试图计算出结果 – Bzinck15105

+1

使用'Split(cellValue, “,”)'从每个单元格创建数组,然后遍历第一个数组中的元素并使用'Match()'来查看它们是否包含在第二个数组中。 –

回答

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 =一; C;,B d

A2 = B; C

A3 = =SET_SUB(A1, A2, ";") =一; d

相关问题