我有一个电子表格,其中B:B列显示超过3万个条目。 我正在尝试找到一种方法: -检查这些单元格中的每个单元格是否都属于“ 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列中的文本(使用数组),并返回一个类别-已经很棒了...
但是,我想知道您的专家是否能够进一步推动这一发展,并且-使用我们的Excel公式VBA-允许我使用多个条件进行这种分类!
公式为=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 ...
此外,第一次是在这里发布-所以请告诉我在撰写本文时是否做错了什么! :)
Txs! M。
https://drive.google.com/open?id=1OceFTFVz_-isGNkBXcKdIY4cxQ4vqKSf
答案 0 :(得分:0)
所以您想遍历恒定范围
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循环。使用字符串中的函数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