我有两个不同的句子。见下面的句子
*,user,rollbacker,accountcreator
*,user,accountcreator,rollbacker,sysop
第一个句子位于单元格A1中,第二个句子位于单元格A2中。
如您所见,第二句有sysop
,这将是不同的。
我想在单元格B2中显示单词sysop
。
我已尝试过TRIM
和SUBSTITUTE
,但由于在第一句rollbacker,accountcreator
和accountcreator,rollbacker
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<ns prefix="m" uri="http://www.ociweb.com/movies"/>
<pattern name="all">
<rule context="m:actor">
<report test="@role=preceding-sibling::m:actor/@role"
diagnostics="duplicateActorRole">
Duplicate role!
</report>
</rule>
</pattern>
<diagnostics>
<diagnostic id="duplicateActorRole">
More than one actor plays the role<value-of select="@role"/>.
A duplicate is named<value-of select="@name"/>.
</diagnostic>
</diagnostics>
</schema>
中句子的排列方式不同,因此无效在第二句话。
有什么建议吗?
谢谢!
答案 0 :(得分:2)
首先用逗号将两个字符串分成两个数组,然后循环遍历每个数组,用空格替换任何相同的单词。然后将两个字符串加在一起并将它们放在所需的单元格中。
像这样:
Dim fArr() As String
Dim SArr() As String
Dim fStr As String
Dim sStr As String
Dim aStr As String
Dim i As Integer, j As Integer
fStr = Range("A1").value
sStr = Range("A2").value
fArr = Split(fStr, ",")
SArr = Split(sStr, ",")
For i = LBound(fArr) To UBound(fArr)
sStr = Replace(sStr, fArr(i) & ",", "")
sStr = Replace(sStr, fArr(i), "")
Next i
For j = LBound(SArr) To UBound(SArr)
fStr = Replace(fStr, SArr(j) & ",", "")
fStr = Replace(fStr, SArr(j), "")
Next j
If Trim(fStr) <> "" And Trim(sStr) <> "" Then
Range("B2") = fStr & "," & sStr
ElseIf Trim(fStr) = "" Then
Range("B2") = sStr
Else
Range("B2") = fStr
End If
答案 1 :(得分:1)
我喜欢Scott的答案,但我在同一时间写了这个更有文字意识的方法:
Public Function CompareCSVStrings(strA As String, strB As String) As String
Dim varA As Variant
Dim varB As Variant
Dim strResults As String
Dim strTest As String
Dim blnDifference As Boolean
Dim intIndexA As Integer
Dim intIndexB As Integer
varA = Split(strA, ",", , vbTextCompare)
varB = Split(strB, ",", , vbTextCompare)
'Look for values in strA that are not in strB
For intIndexA = LBound(varA) To UBound(varA)
vstrTest = varA(intIndexA)
blnDifference = True 'assume not present in second array
For intIndexB = LBound(varB) To UBound(varB)
If StrComp(varB(intIndexB), strTest, vbTextCompare) = 0 Then
blnDifference = False 'this string is not a difference after all
End If
Next intIndexB
If blnDifference Then strResults = strResults & "," & strTest
Next intIndexA
'Look for values in strB that are not in strA
For intIndexB = LBound(varB) To UBound(varB)
strTest = varB(intIndexB)
blnDifference = True 'assume not present in second array
For intIndexA = LBound(varA) To UBound(varA)
If StrComp(varA(intIndexA), strTest, vbTextCompare) = 0 Then
blnDifference = False 'this string is not a difference after all
End If
Next intIndexA
If blnDifference Then strResults = strResults & "," & strTest
Next intIndexB
CompareCSVStrings = strResults
End Function
答案 2 :(得分:0)
这是一种非常直接的方法:
Public Sub ShowDiff()
[b1] = Diff([a1], [a2])
[b2] = Diff([a2], [a1])
End Sub
Private Function Diff(a$, b$) As String
Dim m&, n&
m = 1
a = "," & a & ","
b = "," & b & ","
Do
n = InStr(m + 1, a, ",")
If n Then If InStr(b, Mid$(a, m, n - m + 1)) = 0 Then Diff = Diff & "," & Mid$(a, m + 1, n - m - 1)
m = n
Loop Until n = 0
Diff = Mid$(Diff, 2)
End Function
答案 3 :(得分:0)
使用字典的另一种选择
'Reference Microsoft Scripting Runtime
Dim intCounter1 As Integer
Dim strArr1() As String
Dim strArr2() As String
Dim strDif As String
Dim dict1 As Dictionary
Dim varKey As Variant
strArr1 = Split(Cells(1, 1).Value, ",", , vbTextCompare)
strArr2 = Split(Cells(2, 1).Value, ",", , vbTextCompare)
Set dict1 = New Dictionary
For intCounter1 = LBound(strArr1) To UBound(strArr1)
If dict1.Exists(strArr1(intCounter1)) = False Then
dict1.Add Key:=strArr1(intCounter1), Item:=1
End If
Next intCounter1
For intCounter1 = LBound(strArr2) To UBound(strArr2)
If dict1.Exists(strArr2(intCounter1)) = False Then
If Len(strDif) = 0 Then
strDif = strArr2(intCounter1)
Else
strDif = strDif & ", " & strArr2(intCounter1)
End If
End If
Next intCounter1
Cells(2, 2).Value = strDif