excelexcel-formulaautofill

Excel: Transforming automatically a cell based on the input in another one


I'm creating a database. Column A contains different elements. Column B is a ".type" column, so I need to assign one number to each of those elements and that number needs to be consistent across the whole database, as follows:

Column A     Column B
Cow          1
Chicken      2
Horse        3
Butterfly    4
Cow          1

I need a way to make column B take the number corresponding to each one of the elements of column A. Is there a way to do this with an excel function?

I've tried the IF function, but doesn't seem to work for my purposes, perhaps because I have several elements. It works with only one value and I know which number it's got attributed. For instance: =IF(A5="Cow", "1", "False").

For this reason, I tried to use multiple arguments, as explained here: Change value in a cell based on value in another cell

However, it gave me error. It said that I had used too many arguments. Example to follow:

=IF(A5="Cow", "1", "False", IF(A5="Chicken", "2", "False")).

Is it possible to do what I want to do? For the values in Column B to be automatically populated when an element in Column A is included, while having several (more than 6) elements in Column A?

Thanks in advance for any help.


Solution

  • =LET(a,UNIQUE($A$1:$A$9),b,SEQUENCE(COUNTA(UNIQUE(a)),,1,1),XLOOKUP(A1,a,b,,0))
    

    Drag it down or:

    =LET(c,A1:A9,a,UNIQUE(c),b,SEQUENCE(COUNTA(UNIQUE(a)),,1,1),MAP(c,LAMBDA(z,XLOOKUP(z,a,b,,0))))
    

    Result:

    enter image description here