Set cell value based on value in other cell

2019-09-10 03:12发布

问题:

I have a following table structure:

    --- c1 ------- c2 ---- c3 ------ c4
 r1: **0001**  **word1**
 r2:   0002      word2  **0001**  **word1**
 r3:   0003      word3    0002      word2

I would like to set column three values automatically, so that if there is some word in column 4, then program should check column 2 and try to find the same word. If found, then column 1 value should be used as a column 3 value.

I could do that manually, but it's more work and it gets problematic if I ever add new rows. Column 1 value is equal to row number, so it's updated automatically if new rows added. Now I would like to set my data so that also column 3 would be updated automatically in those cases.

What is the right formula to make that happen?

回答1:

The formula you want is:

=IF(D1<>"",INDEX(A:A,MATCH(D1,B:B,0)),"")


EDIT

For structured table references:

=IF([@Col4]<>"",INDEX([Col1],MATCH([@Col4],[Col2],0)),"")



回答2:

If you slightly rearrange your worksheet, then the VLOOKUP() function can be used. I swapped your A and B (first and second) columns to arrive at this:

  --- A ----  B  ---- C  ---  D
r1: word1    0001
r2: word2    0002    0001   word1
r3: word3    0003    0002   word2

Enter into C1:

=VLOOKUP(D1, A$1:B$3, 2, FALSE)

and copy this formula down the C column.

The reason for swapping the first two columns is that VLOOKUP() always looks in the first column for the match.