0

我正在尝试进行反向查找。

有:句子(字符串) 寻找:关键字(数组)

我知道我可以用 if(iser(find("Missing",B1,1)),if(isrr(find("Located",B1,1)),.. 做一个相当疯狂的复合语句。

但是考虑到我需要使用这个 1000 次,并且 if then's 将是 18 深。那是没有意义的。我知道我可以很清楚地用 VBA 做到这一点,但我似乎无法弄清楚语法。

这是我到目前为止所拥有的:

Function FindValue(ByRef strToSearch As String, rngLookUpValues As Range) As String
On Err GoTo err_capture
'strToSearch is the sentence I am searching
'rngLookUpValue is a two column Range.
'      The first column is what I'm searching for.  If it exists in the sentence, 
'               return the second column
'      The second column is the category that applies when the word from column one 
'                is found in the sentence

i = 0
For Each row In rngLookUpValues
    i = i + 1
    If InStr(1, strToSearch, row.cell(i, 1).Value, vbTextCompare) > 0 Then
        FindValue = row.cell(i, 2).Value
    End If
Next

Exit Function
err_capture:
    Debug.Print Err.Number & ": " & Err.Description
End Function

当我运行它时,它返回一个#Value。但是当我调试它或观察它运行时,没有错误。它只是在 instr() 函数期间死亡。

4

1 回答 1

2

不需要VBA,一个数组公式就可以了:

=IFERROR(INDEX($B:$B,MATCH(TRUE,FIND($A:$A,D1)>0,0)),"")

其中 columnB是类别, columnA是单词列表和D1要检查的句子。

注意:用Ctrl- Shift-输入数组公式Enter

对于 Excel 2003 和您的特定示例,请使用以下公式:

=IF(ISERROR(MATCH(TRUE,FIND('Intake Chart'!$A$2:$A$18,E26)>0,0)),"不匹配",
INDEX('摄入量表'!$B$2:$B$18,MATCH(TRUE,FIND('摄入量表'!$A$2:$A$18,E26)>0,0)))

看看这个文件- 它具有完全相同的结构和工作公式。

于 2013-01-31T21:16:33.767 回答