【问题标题】:Compare a substring array in string array in excel在excel中比较字符串数组中的子字符串数组
【发布时间】:2016-11-07 07:48:33
【问题描述】:

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

      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。如果 A1 列的任何子字符串确实存在于 A2 的任何字符串中,则 A3 将显示 TRUE

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

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

但结果并非 100% 正确。任何内置公式都可以做到这一点? (无宏)

【问题讨论】:

  • here
  • @Blueblazer172 以前用过,一直显示VALUES和NAS
  • A1 究竟包含什么?您似乎在同一单元格中都有三个值,由换行符分隔。但是,A2 中的条目是否也包含在单个单元格中?如果是这样,为什么“单元格”A3 中有三个结果?

标签: excel excel-formula worksheet-function


【解决方案1】:

您可以使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2022-07-05
    相关资源
    最近更新 更多