如何用一个值replace多个相似的值

Public Function SameStuff(s1 As String, s2 As String) As Boolean Dim bad As Boolean SameStuff = False ary1 = Split(Replace(s1, " ", ""), ",") ary2 = Split(Replace(s2, " ", ""), ",") Length1 = UBound (ary1) Length2 = UBound(ary2) k=1 If Length1<= Length2 and Length1<>0 then for i=0 to Length1-1 If ary1(i) = ary2(i) then ary3(k,i) = ary1(i) End If Next i k=k+1 else Exit function End If End Function 

这里我从Range(“A1”) – (有3个单词)和Range(“A2”) – (有4个单词)的值中取值。 通过在单词之间find空格并将它们存储在数组中来拆分它们。 如果一个数组的长度是3,另一个是4,那么将会比较两个数组中的3个字。 如果发现3个单词相同,则范围(“B1”)和范围(“B2”)都必须具有3个单词的名称,即范围(“A1”)。 我认为这个逻辑可以很好地在A1中find类似的名字,例如“ABC DEF HIJE”,在A2中find“ABC DEF HI HIJ Limited”。

我不能把它放在代码中。

字长不会保持不变,即3,4。

使用字典将是一个简单的select,你可以使用.exists方法为你做这个,你必须将数组(split()的结果)传递给字典,但是这是一个循环,不是太棘手。 或者,你可以将其中一个inputas作为一个string,并且只拆分1, if strStringLeftAlone like "* " & strSection(x) & " *"使用,或者使用instr,和search" " & strSection(x) & " "或find

无论数组有多长,无论有多less个单词(和空格)存在于每个要比较的string中,这都应该起作用。 注意我删除了kvariables,因为它在代码中似乎没有任何用处。 但是,这个解决scheme确实假设,只有两个string中的最后一个字是不同的。

 Public Function SameStuff(s1 As String, s2 As String) As Boolean Dim sameBool As Boolean Dim i As Long, Length1 As Long, Length2 As Long Dim tempArr1 as String, tempArr2 as String Dim ary1 as Variant, ary2 as Variant ary1 = Split(Replace(s1, " ", ""), ",") ary2 = Split(Replace(s2, " ", ""), ",") Length1 = UBound (ary1) Length2 = UBound(ary2) If Length1 <= Length2 and Length1 > 0 then For i=0 to Length1-1 tempArr1 = tempArr1 & ary1(i) tempArr2 = tempArr2 & ary2(i) Next i If tempArr1 = tempArr2 then sameBool = True End If SameStuff = sameBool End Function 

编辑

在我忘记的代码中添加了一些variables声明,否则代码将不能在模块顶部的Option Explicit中使用。