简体   繁体   中英

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. 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.

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.

Is there a way to do using excel formulas and not 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

Step 2: Copy A6 to B6:G6, A7:A10 and 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):

=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

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. The -2 is important because there are two header columns to the left of the data.

=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.

=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

=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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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