如何檢查單個值以查看它是否包含 18 個值之一 (How do I check a single value to see if it contains one of 18 values)


問題描述

如何檢查單個值以查看它是否包含 18 個值之一 (How do I check a single value to see if it contains one of 18 values)

I'm trying to do a backwards vlookup.

Have:  Sentences (string) Looking for:  Keywords (array)

I know I could do a rather crazy compound statement with      if(iserr(find("Missing",B1,1)),if(iserr(find("Located",B1,1)),..

But considering I need to use this 1,000 times, and the if then's would be 18 deep. that just doesn't make sense. I know I can do this with VBA pretty clealy, but I can't seem to figure out the syntax.

Here's what I have so far:

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

When I run this it returns a #Value.  But when I debug it or watch it run, no errors.  It just dies during the instr() function.

‑‑‑‑‑

參考解法

方法 1:

No need for VBA, an array formula will do:

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

where column B is the category, column A the list of words and D1 the sentence to check.

Note: Enter the array formula with Ctrl‑Shift‑Enter!

For Excel 2003 and your particular example, use this formula:

=IF(ISERROR(MATCH(TRUE,FIND('Intake Chart'!$A$2:$A$18,E26)>0,0)),"no match",
INDEX('Intake Chart'!$B$2:$B$18,MATCH(TRUE,FIND('Intake Chart'!$A$2:$A$18,E26)>0,0)))

Take a look at this file ‑ it has exactly the same structure and the working formula.

(by Lee_StrPeter Albert)

參考文件

  1. How do I check a single value to see if it contains one of 18 values (CC BY‑SA 3.0/4.0)

#range #lookup #vba






相關問題

tcng - 匹配“if”語句中的端口範圍 (tcng - match port range in "if" statement)

如何在 MATLAB 中將自定義顏色範圍與顏色條相結合? (How to combine a custom color range with colorbar in MATLAB?)

如何檢查單個值以查看它是否包含 18 個值之一 (How do I check a single value to see if it contains one of 18 values)

選擇單元格時獲取 excel 以運行 vba 代碼的問題 (Problems getting excel to run vba code when a cell is selected)

對包含 EXCEL 中文本的單元格進行數學運算 (Do math on cells also contaning text in EXCEL)

autohotkey:添加連續的數字範圍 (autohotkey: add consecutive range of numbers)

如何將一種功能集成到另一種功能中 (How to integrate one function into another one)

使用活動單元格查找範圍名稱 (Find Range Name Using Active Cell)

基於 C++ 非迭代器的範圍庫? (C++ non-iterator based range library?)

用D語言從一個範圍內製作數組 (Make array from a range in D language)

Internet Explorer 上的自定義範圍滑塊問題 (Custom range slider issue on Internet Explorer)

如何從列表中刪除一系列值並使上述減去的範圍在新列表中不可用? (How to delete a range of values from a list and make the aforementioned subtracted range unavailable in a new list?)







留言討論