在一个单元格内进行多次搜索,如果找到则偏移结果

multiple search within a cell and offset result if found

我有一个电子表格,其中 B:B 列显示超过 30k 个条目。 我正在尝试找到一种方法: - 检查这些单元格中的每一个是否作为范围 "content1" 中的内容 - 如果是这样,那么还要检查同一个单元格是否也有范围 "content2" 与第一个相邻的内容(但范围不一定与 "content1" 长度相同,并且如果可能的话,不一定存在; - 理想情况下可以添加更多范围来搜索... 如果在列 B:B 的单元格中找到 range1 AND 2 [AND x] 的内容,则获取显示 "content1"..."content2" 的第一个单元格右侧的单元格内容...并将其写在公式所在的位置... 可能更容易展示一个例子:

Colum B:B                      Range1    Range2   Range3  Rangexx  Result
The quick brown fox jumps      fox       brown     quick  jumps    Fast Animal               
The green tree moves slowly    tree      green     fast            Green Vegetal                                   
The brown tree moves slowly    tree      brown     slow            Brown Vegetal                                        
The green house in the tree    house     green                     House green                                                                                  
Hitchhiker guide to the galaxy galaxy    guide                     Space                                                     

结果将是:

Column B:B                           Column C:C
The quick brown fox jumps            Fast Animal
The green tree moves slowly          ""
The brown tree moves slowly          Brown Vegetal
The green house in the tree          House green                                              
Hitchhiker guide to the galaxy       Space

我想要实现的是使用多个标准对大量单词进行分类...

我发现(并测试了)一个公式,它允许我根据一个标准和 return 一个类别测试列 B:B 中的文本(使用数组)——这已经很棒了...

但想知道你们这些专家是否真的能够进一步推动这一点,并且 - 使用 VBA 我们的 excel 公式 - 请允许我使用多个标准进行分类!

公式为=INDEX(result_text;MATCH(TRUE;ISNUMBER(SEARCH(search_text;B2));0))

result_text访问的偏移类是B2中search_text的搜索成功! :)

我还发现了一个 VBA 宏,它似乎与我想要实现的目标相差无几,但我的 VBA 技能太有限,无法适应它(搜索和循环似乎已经存在): ...

此外,我是第一次 post 来这里 - 如果我在写这篇 post 时做错了什么,请告诉我! :)

谢谢! M.

例子 link to exemple

https://drive.google.com/open?id=1OceFTFVz_-isGNkBXcKdIY4cxQ4vqKSf

所以你想遍历你的常量范围

dim cell as Range
dim myRange as Range

myRange = yourContantRangeDefinedHere

    ' Loop through each cell in the range
    for each cell in myRange
        'If the the cell in the next column to the right isn't empty AND the cell two columns over isn't empty
        if cell.offset(0, 1).value <> "" and cell.offset(0,2).value <> "" then
            'Do all the things here
            'Assuming your example the result is 5 columns to the right of B:B
            Msgbox cell.offset(0,5).value
        end if
    next

如果您随后尝试在第一个条件内搜索另一个 table,那么您可以在第一个循环内嵌套另一个 for 循环。使用 in string 函数 INSTR 其中 returns 它在较大字符串中找到您的搜索字符串的整数,如果找不到它那么它 returns 0.

dim table1Cell as Range
dim table1Range as Range
dim table1Cell as Range
dim table1Range as Range

table1Range = yourContantRangeDefinedHere
table2Range = yourSecondTableRangeHere

    ' Loop through each cell in the range
    for each table1Cell in table1Range

        ' Then we loop through the second table within the first loop
        for each table2Cell in table2Range
            'Then we will search the table1Cell value and see if it contains what is in the table2Cell value and the next column in table2

            if INSTR(1, table1Cell.value, table2Cell.value) > 0 and INSTR(1, table1Cell.value, table2Cell.offset(0,1).value) > 0 then
                'Both INSTR searches have returned a value greater than 0 so both have found matches, now you can get the value of the result column in table2 and return it to table1

                table1Cell.offset(0, 7).value = table2Cell.offset(0, 2).value
            end if
        next table2Cell
    next table1Cell