Home » excel » MS Excel 2012 – Replace value if equal to other

MS Excel 2012 – Replace value if equal to other

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a column with sample data(duplicate):

A10
B20
C30
A10
C30
C30
...

I want to replace each value with ID hold in other columns:

A10 - ID1
B20 - ID2
C30 - ID3

I have to many id’s to do this with the ordinary replace. Is there a tricky way to do this or I should use other technique (like move the data in some program language and to do the replacement there).

Thank you in advance.
Joro

How to&Answers:

You can do it without any VBA, using VLOOKUP. Put all your key/value pairs in a 2-column array, e.g., D1:E3. Assuming your column of data starts in A1, put this formula in column B1:

= VLOOKUP(A1, $D$1:$E$3, 2, FALSE)

then copy-paste down.

If not all your original values are mapped into a new ID, the VLOOKUP will return ‘#N/A’. If you want to keep the original values for these, your formula will be a bit more complex:

= IF(ISNA(VLOOKUP(A1, $D$1:$E$3, 2, FALSE), A1, VLOOKUP(A1, $D$1:$E$3, 2, FALSE)))

Answer:

Here’s a macro for searching for a series of values/strings and replacing them with a new set of values/strings:


Mass Replace


There’s a sample sheet there you can use to test it out, too.

To use the macro as is:

  1. create a new sheet called Categories
  2. put the values to find in column A
  3. put the replacement values in column B
  4. Bring up your main sheet onscreen (sample sheet it’s called Data)
  5. Run the macro