【问题标题】:multiple search within a cell and offset result if found在一个单元格内进行多次搜索,如果找到则偏移结果
【发布时间】:2019-01-26 13:35:39
【问题描述】:

我有一个电子表格,其中 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

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

我找到(并测试了)一个公式,它允许我根据一个标准测试 B:B 列中的文本(使用数组)并返回一个类别 - 这已经很棒了...

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

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

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

我还发现了一个 VBA 宏,它似乎与我想要实现的目标相距不远,但我的 VBA 技能太有限而无法适应它(搜索和循环似乎已经存在):Search Multiple different string in excel VBA .. .

另外,我第一次在这里发帖 - 所以请告诉我我在写这篇文章时是否做错了什么! :)

发送! M.

例子 link to exemple

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

【问题讨论】:

  • 我不确定,但不会 =INDEX(result_text;MATCH(TRUE;AND(ISNUMBER(SEARCH(search_text1;B2);ISNUMBER(SEARCH(search_text2;B2)));0) ) 工作吗?
  • txs 回复我!让我试试:)
  • OK - 不起作用,因为您的公式每次应该循环遍历该范围中的每个项目时,只取两个范围中的每一个的第一个元素。我需要公式来依次查找每个元素,并检索其中包含前 2 个(或更多,如果更多)值的那个...布列斯特国际中学收费标准 国际中学伏尔泰 奥尔良国际中学收费标准
  • 我已在 mt 初始帖子中添加了一个示例,以尝试阐明要实现的目标... :)
  • 我目前无法访问 excel 来测试任何东西,但这样的事情应该接近正确的解决方案:=INDEX(result_text;MATCH(1;SUMPRODUCT(ISNUMBER(SEARCH(search_range1) ;B2);ISNUMBER(SEARCH(search_range2;B2)));0))。您可能需要将其作为数组公式输入,但不确定。

标签: excel vba excel-formula excel-2016


【解决方案1】:

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

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

如果您随后尝试在第一个条件内搜索另一个表,那么您可以在第一个表中嵌套另一个 for 循环。使用 in string 函数 INSTR 它返回一个整数,表示它在较大的字符串中找到您的搜索字符串,如果没有找到它则返回 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

【讨论】:

  • 你好 iShaymus!发送您的帖子,因为它解决了我的部分难题! :) 接下来要解决的是,在该循环中,取循环中第一个单元格的内容,在“B”列中搜索,然后,找到时,搜索下一个搜索列中的单词是否为ALSO 在 B 列中找到,然后如果找到,并且如果有第 3 列,则检查它是否也找到 - 如果是,然后阅读列中的条目并复制结果...我添加了一个电子表格示例,以尝试阐明要实现的目标:)
  • 我已经更新了我原来的答案。请理解 SO 不是代码编写服务,您应该尝试自己编写,然后在遇到困难时寻求帮助。如果这回答了您的问题,那么请将问题标记为已回答。
  • 你好 iShaymus!完全理解 - 尝试编写那段代码,但我的 VBA 技能还不够好......编写了迄今为止的代码来管理内容/自动分类单元格的内容,但仅使用单一测试(使用我首先使用的功能在我的声明中提到):) 还没有达到循环和多次搜索:上周花了 2 天试图自己解决它,没有运气 - 并且不想让自己尴尬地展示任何有经验的人都会嘲笑的代码! :)
  • 所以 - 非常感谢您整理出这个:将尝试一下并用它来理解循环/搜索!再次感谢...
  • 请将问题标记为已回答,以帮助其他人日后找到答案。
猜你喜欢
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2014-09-13
  • 1970-01-01
相关资源
最近更新 更多