简体   繁体   English

Excel数组公式,仅复制包含字符串的单元格

[英]Excel array Formula that copies only cells containing a string

I would prefer to use an excel array formula (but if it can only be done in VBA, so be it) that copies ALL cells from a column array that contains specific text. 我更喜欢使用一个Excel数组公式(但是,如果只能在VBA中完成该操作),它将从包含特定文本的列数组中复制所有单元格。 The picture below shows what I am after and what I have tried. 下图显示了我的追求和尝试过的东西。 I'm getting close (thanks to similar, but different questions) but can't quite get to where I want. 我正在接近(由于相似但不同的问题),但无法完全到达我想要的位置。 At the moment, I am getting only the first cell instead of all the cells. 此刻,我只获得第一个单元格,而不是所有单元格。 In my actual application, I am searching through about 20,000 cells and will have a few hundred search terms. 在我的实际应用中,我正在搜索大约20,000个单元格,并且将有数百个搜索词。 I expect most search terms to give me about 8 - 12 cells with that value. 我希望大多数搜索字词都能为我提供大约8到12个具有该值的单元格。

formula I am using: 我正在使用的公式:

=INDEX($A$4:$A$10,MATCH(FALSE,ISERROR(SEARCH($C$1,$A$4:$A$10)),0))

Spredsheet Image Spredsheet图片

To make this work efficiently, I recommend having a separate cell holding the results count (I used cell C2) which has this formula: 为了使这项工作有效,我建议使用一个单独的单元格保存结果计数(我使用单元格C2),该单元格具有以下公式:

=COUNTIF(A:A,"*"&C1&"*")

Then in cell C4 and copied down use this array formula (The -3 is just because the header row is row 3. If the header row was row 1, it would be -1 ): 然后在单元格C4中向下复制并使用此数组公式( -3只是因为标题行是第3行。如果标题行是第1行,则将是-1 ):

=IF(ROW(A1)>$C$2,"",INDEX($A$4:$A$21000,SMALL(IF(ISNUMBER(SEARCH($C$1,$A$4:$A$21000)),ROW($A$4:$A$21000)-3),ROW(C1))))

I tested this with 21000 rows of data in column A with an average of 30 results per search string and the formula is copied down for 60 cells in column C. With that much data, this takes about 1-2 seconds to finish recalculating. 我用A列中的21000行数据进行了测试,每个搜索字符串平均有30个结果,并且该公式在C列中向下复制了60个单元格。使用这么多数据,这大约需要1-2秒才能完成重新计算。 Recalculation time can vary widely depending on other factors in your workbook (additional formulas, nested dependencies, use of volatile functions, etc) as well as your hardware. 重新计算时间的长短取决于工作簿中的其他因素(附加公式,嵌套的依赖项,易失函数的使用等)以及您的硬件。

Alternately, you could just use the built-in Filter functionality, but I hope this helps. 或者,您可以只使用内置的“过滤器”功能,但是希望对您有所帮助。

You need to get the ROWS. 您需要获得ROWS。 Put this in C4 and copy down. 将其放在C4中并复制下来。

=IFERROR(AGGREGATE(15,6, IF(SEARCH($C$1, $A$4:$A$10)>0, ROW($A$4:$A$10)), ROW($C4)-ROW($A$4)+1), "")

Array formula so use ctrl-shift-Enter 数组公式,因此请使用ctrl-shift-Enter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM