简体   繁体   中英

How to replace comma separated values to comma separated ids in the same/individual cell in excel?

I have a table where Products are mapped with ingredients and another table where Ingredients are mapped with IDs like this below :

在此处输入图片说明

Below is what I need :

在此处输入图片说明

Here I want to replace the comma separated ingredients values in "Data Table" with their IDs from "Master Table".

I checked out Replace Function to accomplish this task but couldn't manage to do it somehow.

=REPLACE(C2,1,LEN(C2),..

Here C2 ie Ingredient column in "Data Table" doesn't have single value to replace it easily, they are available in combination for which replace statement is not the correct solution although the same can be handled programmatically but I want to do it in excel way.

It would be very grateful if someone can guide me with solution to this problem.

Thanks in advance.

This is not a direct solution nor a VBA solution but through a manual action and 2 simple formula's, this is possible.

I used a combination of Data>Text to Columns , VLOOKUP and Concatenate functions to achieve your result table.

Step 1.: Delimit your data using Text To Column .

Step 2.: Add VLOOKUP Functions for each ingredients.

Step 3. Use the Concatenate function to bring all the VLOOKUPS together.

You can find my solution using your example here in a Google sheet:

The only thing I can come up with is a nested SUBSTITUTE function like this one:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(C1,D1,E1),D2,E2),D3,E3),D4,E4)

Here your Ingredients are in Column D and their respective IDs in Column E (ie your Master table in Sheet 2).

As you probably have many ingredients, you can use CONCATENATE to create the correct formula. Assuming your have your recipe in column E , your ingredients in column F and the corresponding ingredient IDs in column G :

  • Write SUBSTITUTE(E2,F2,G2) into D1 (where E2 , corresponds to the first entry in the Ingredients column in your data table, F2 corresponds to your first ingredient in your Master table, and G2 corresponds to the related first Ingredient ID in the Master table)
  • Write SUBSTITUTE into A2
  • Write =ADDRESS(ROW(F3),COLUMN(F3),4) into B2 (where F3 references your second ingredient in your Master table)
  • Write =ADDRESS(ROW(G3),COLUMN(G3),4) into C2 (where G3 references the corresponding ID to the second ingredient in your Master table)
  • Write =CONCATENATE(A2,"(",D1,",",B2,",",C2,")") into D2
  • Drag A2:D2 down as far as necessary

You now have the necessary formula. Copy, paste the last entry, selecting Paste Values and set a = in front.

在此处输入图片说明

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