比较excel中字符串数组中的子字符串数组

时间:2016-11-07 07:48:34

标签: excel excel-formula worksheet-function

所以我有一个表,第一列是子串数组,第二列是字符串数组:

      A1                  A2                          A3
----------------------------------------------------------
I am a police   ||   My name is Jean             ||  TRUE
Nothing mine    ||   I am a police and a cook    ||  TRUE
is              ||   bla bla bla                 ||  FALSE

所以它将比较A2和A1。如果A2中任何字符串中的任何子字符串确实存在,则A3将显示TRUE

我正在使用SUMPRODUCT,SEARCH和ISNUMBER,如:

=SUMPRODUCT(--ISNUMBER(SEARCH(A1,A2)))>0

但结果并非100%正确。公式中的任何构建都可以这样做吗? (没有宏)

1 个答案:

答案 0 :(得分:0)

您可以使用VBA代码执行此操作。 添加新模块并输入以下代码:

Sub matchIt()

Dim lastRow_String As String
Dim lastRow_SubString As String


With ActiveSheet
  lastRow_String = .Cells(.Rows.Count, "B").End(xlUp).Row
  lastRow_SubString = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For i = 1 To lastRow_String ' B string

    For j = 1 To lastRow_SubString 'A substring

        If InStr(1, ActiveSheet.Cells(i, 2).Value, ActiveSheet.Cells(j, 1).Value) > 0 Then

            ActiveSheet.Cells(i, 3).Value = "True"

        End If

        If ActiveSheet.Cells(i, 3).Value = "" Then
            ActiveSheet.Cells(i, 3).Value = "False"
        End If

    Next j

Next i


End Sub