我有一个包含字母数字数据的大型Excel电子表格。我希望能够并排比较不同行中的两个单元格,并返回另一个单元格中的差异。
e.g。我有B2," tom,rick,mike,我"在C2单元格中," mike,rick"。我需要 比较单元格C2和单元格B2并返回单元格D2中的差异,在这种情况下,单元格D2将是字符"汤姆,我"。它们与","分开。它们可以按照您在示例中看到的顺序不同。
答案 0 :(得分:0)
将每个字符串拆分为项目列表。将第一个列表中的每个项目与第二个列表进行比较,将第二个列表中不存在的项目添加到第三个列表中。您可能需要将其展开以使用包含空格和逗号的列表。
Public Function ListDiff(names1 As String, names2 As String) As String
Dim list1() As String
Dim list2() As String
list1 = Split(names1, ",")
list2 = Split(names2, ",")
list3 = ""
For i = LBound(list1) To UBound(list1)
If Not existsInList(list2, list1(i)) Then
list3 = list3 & "," & list1(i)
End If
Next i
'remove leading comma
ListDiff = Right(list3, Len(list3) - 1)
End Function
Public Function existsInList(list() As String, item As String) As Boolean
exists = False
i = LBound(list)
bFound = False
While i <= UBound(list) And Not bFound
If list(i) = item Then
bFound = True
End If
i = i + 1
Wend
existsInList = bFound
End Function