简体   繁体   English

连续查找字符串并在Excel中将其删除

[英]Finding a string in a row and removing it in excel

I have a table similar to the one below 我有一张类似于下面的表格

si  id  a   b   c   d   e
1   123 abc bcd abc def efg
2   234 bcd cde def efg fgh
3   345 cde efg efg abc ghi

What I want to do is find if the string "abc" is present in a row and if present remove it and copy the succeeding columns to its position ie in the first row "abc" is present in column A, so i want to remove it and move col BE to Col AD. 我想做的是查找字符串“ abc”是否存在于行中,如果存在,请将其删除,然后将后续列复制到其位置,即第一行“ abc”存在于列A中,所以我要删除并将col BE移至Col AD​​。 In the 2nd row "abc" is not present, so this row should be retained as it is but in the 3rd row, "abc" is in Col D so it should be removed and "ghi" should be pasted in its location thus leaving col E empty. 在第二行中不存在“ abc”,因此应按原样保留该行,但在第三行中,“ abc”在Col D中,因此应将其删除,并将“ ghi”粘贴到其位置,从而保留列E空。

I was able to do this using the MATCH but the catch here is MATCH only finds the first occurence but not the succeeding ones. 我能够使用MATCH做到这一点,但这里的问题是MATCH仅找到第一个出现的事件,而没有找到后续出现的事件。

Is there a way to do using excel formulas and not vba. 有没有一种方法可以使用excel公式而不是vba。

You cannot exit the data in place with formulas (mentioned in the comments) - but if you want a copy of the data that automatically removes the items updates the table whenever the original table is modified you can do that. 您无法使用公式(在注释中提及)就地退出数据-但是,如果您想要自动删除项目的数据副本,则只要修改原始表就可以更新该表。 I will stick the result data below the input data but you could stick it on another sheet if you wanted to... 我会将结果数据粘贴在输入数据下方,但是如果您愿意,可以将其粘贴在另一张纸上。

We start with this: 我们从这个开始:

   |   A   |   B   |   C   |   D   |   E   |   F   |   G   |
---+-------+-------+-------+-------+-------+-------+-------+--
 1 |    si      id       a       b       c       d       e
 2 |     1     123     abc     bcd     abc     def     efg
 3 |     2     234     bcd     cde     def     efg     fgh
 4 |     3     345     cde     efg     efg     abc     ghi
 5 |
 6 |
 7 |
 8 |
 9 |
10 |

Step 1: in A6 put =A1 步骤1:在A6中放入=A1

Step 2: Copy A6 to B6:G6, A7:A10 and B7:B10 步骤2:将A6复制到B6:G6,A7:A10和B7:B10

Now the sheet looks like this: 现在工作表如下所示:

   |   A   |   B   |   C   |   D   |   E   |   F   |   G   |
---+-------+-------+-------+-------+-------+-------+-------+--
 1 |    si      id       a       b       c       d       e
 2 |     1     123     abc     bcd     abc     def     efg
 3 |     2     234     bcd     cde     def     efg     fgh
 4 |     3     345     cde     efg     efg     abc     ghi
 5 |
 6 |    si      id       a       b       c       d       e
 7 |     1     123
 8 |     2     234
 9 |     3     345
10 |

Step 3: In C7 enter this array formula (remember to press Alt-Shift-Enter instead of just pressing Enter): 步骤3:在C7中输入以下数组公式(请记住按Alt-Shift-Enter而不是仅按Enter):

=IFERROR(INDEX($C2:$G2,1,SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1))),"")

Step 4: copy C7 to D7:G7 and C8:G9 步骤4:将C7复制到D7:G7和C8:G9

Now the sheet looks like this: 现在工作表如下所示:

   |   A   |   B   |   C   |   D   |   E   |   F   |   G   |
---+-------+-------+-------+-------+-------+-------+-------+--
 1 |    si      id       a       b       c       d       e
 2 |     1     123     abc     bcd     abc     def     efg
 3 |     2     234     bcd     cde     def     efg     fgh
 4 |     3     345     cde     efg     efg     abc     ghi
 5 |
 6 |    si      id       a       b       c       d       e
 7 |     1     123     bcd     def     efg
 8 |     2     234     bcd     cde     def     efg     fgh
 9 |     3     345     cde     efg     efg     ghi
10 |

How it works: 这个怎么运作:

=IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2) generates an array of values with a number wherever a value is not abc and the value FALSE wherever a value is abc. =IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2)生成一个值数组,该数组的数值为非abc的值,而数值为FALSE的值为abc。 The -2 is important because there are two header columns to the left of the data. -2很重要,因为数据左侧有两个标题列。

=SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1)) returns the nth smallest number in the array that was returned - it skips all the FALSE values. =SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1))返回返回数组中的第n个最小数字-跳过所有FALSE价值观。

=INDEX($C2:$G2,1,SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1))) returns the value the location of that nth smallesst number - if there is no value there because of the filtered out items it returns an error =INDEX($C2:$G2,1,SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1)))返回该值的位置第n个Smallestst数字-如果由于过滤出的项目而没有值,则返回错误

=IFERROR(INDEX($C2:$G2,1,SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1))),"") returns the value and replaces the errors with an empty cell. =IFERROR(INDEX($C2:$G2,1,SMALL(IF($C2:$G2<>"abc",COLUMN($C2:$G2)-2),COLUMN(A1))),"")返回值并将错误替换为空单元格。

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

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