问题
So currently I have two DATA inputs in excel Data 1 and Data 2 I need a formula or some sort to display results *currently this is a manual process.
回答1:
Approach using Filter()
function
Function test(ByVal a, ByVal b, Optional Delim$ = ",") As String
'Purpose: return non unique values by comparing two comma separated string lists a vs. b
a = Split(a, Delim): b = Split(b, Delim)
Dim elem
For Each elem In b
a = Filter(a, elem, False) ' Include:=False, i.e. exclude non uniques
Next elem
test = Join(a, Delim & " ")
End Function
Argument a: 668,669,777,778,779,780,781,782,891,893,894,895
Argument b: 668,777,779,778,780,892,891,782
Result string 669, 781, 893, 894, 895
BTW the result shouldn't include 782 (c.f. OP) as this isn't a unique item.
Caveat/Edit
The above approach assumes three figure numbers only. As basically the Filter
function executes a partial string search this would lead to unwanted results if you would be searching for e.g. 7
thus excluding any number containing 7
, as well. ~~> See revised code
Revised code
Allows only full string matches:
Variant a) 2nd list contains doublettes, but is only a sub set of 1st list
Function test(ByVal a, ByVal b, Optional Delim$ = ",") As String
'Purpose: return non unique values by comparing two comma separated string lists a vs. b
a = "$" & Replace(a, Delim, "$" & Delim & "$") & "$"
a = Split(a, Delim): b = Split(b, Delim)
Dim elem
For Each elem In b
a = Filter(a, "$" & elem & "$", False) ' Include:=False, i.e. exclude non uniques
Next elem
test = Replace(Join(a, Delim & " "), "$", vbNullString)
End Function
Variant b) 2nd list contains also new numbers diferring from 1st list
This might be the waterproof solution to your question (results corresponding to @RonRosenfeld 's fine solution).
Function test(ByVal a, ByVal b, Optional Delim$ = ",", Optional ByVal cnt% = 1) As String
'Purpose: return non unique values by comparing two comma separated string lists a vs. b
If cnt = 1 Then ' recursive call to check 2nd string against 1st one
test = test(b, a, Delim, 2)
If Len(test) > 1 Then test = Mid(test, 1, Len(test) - 1)
End If
a = "$" & Replace(a, Delim, "$" & Delim & "$") & "$"
a = Split(a, Delim): b = Split(b, Delim)
Dim elem
For Each elem In b
a = Filter(a, "$" & elem & "$", False) ' Include:=False, i.e. exclude non uniques
Next elem
test = Replace(Join(a, Delim), "$", vbNullString) & Delim & test
If cnt = 0 Then test = Replace(test, Delim, Delim & " ") ' add blank after delimiters
End Function
回答2:
If you are using Excel 2016 or Office 365 with the TEXTJOIN
function, you can do this with an array formula
=TEXTJOIN(", ",,FILTERXML("<t><s>" & SUBSTITUTE(SUBSTITUTE(TEXTJOIN(",",TRUE,Data_1,Data_2)," ",""),",","</s><s>") & "</s></t>","//s[not( .=preceding::*) and not(.=following::*)]"))
Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...}
around the formula as observed in the formula bar
TEXTJOIN
the strings using the comma delimiter- If there are only two or three strings, you could use a simple concatenation and avoid the
TEXTJOIN
function, but you'd still need Excel 2013+ to have theFILTERXML
function
- If there are only two or three strings, you could use a simple concatenation and avoid the
- Remove the spaces. If all delimiters are
<comma><space>
we could simplify the formula a bit. - Create an XML using the
<comma>
to define the nodes. - the
xPath
says to return all nodes that are not followed by or preceded by it's duplicate. TEXTJOIN
the resultant array
回答3:
Function TEST(aString As String, aString2 As String, Optional Delimiter As String = ",") As String
array1 = Split(aString, Delimiter)
array2 = Split(aString2, Delimiter)
Dim i 'As Integer
Dim j 'As Integer
Dim isFound 'As Boolean
Dim output
For i = 0 To UBound(array1)
isFound = False
For j = 0 To UBound(array2) - 1
If array1(i) = array2(j) Then
isFound = True
End If
Next 'j
If Not isFound Then
output = output + array1(i) & ", "
End If
Next 'i
TEST = output
End Function
来源:https://stackoverflow.com/questions/58677041/vba-excel-how-to-display-non-equal-values-in-an-excel-array