即使值不是相同的顺序,也要比较两个单元格中的值

时间:2016-05-28 18:44:11

标签: excel-vba excel-formula vba excel

我有两列A和B.单元格A1中的值是“abc,def,ghi”,单元格B1中的值是“ghi,abc,def”。单元格包含相同的值。但是,顺序不一样。如果使用A1 = B1,我会得到假。我该如何做对。

1 个答案:

答案 0 :(得分:3)

尝试以下用户定义函数:

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, " ", ""), ",")
    If UBound(ary1) <> UBound(ary2) Then Exit Function

    For Each a1 In ary1
        bad = True
        For Each a2 In ary2
            If a1 = a2 Then bad = False
        Next a2
        If bad = True Then Exit Function
    Next a1
    SameStuff = True
End Function

例如:

enter image description here

<强> 注意:

  • space 字符被丢弃
  • 每个单元格中可以有三个以上的项目。
  • TRUE表示 B1 中的项目是 A1
  • 中项目的排列