HomeGoogle DocsSpreadsheetReplace Multiple Comma Separated Values in Google Sheets

Replace Multiple Comma Separated Values in Google Sheets

Published on

Replace, Substitute, and Regexreplace are the text functions to replace text strings in Google Sheets. But sometimes you can use Vlookup too! You can replace multiple comma-separated values in Google Sheets with other values in the following ways.

  1. Using a nested Substitute formula.
  2. Using a Vlookup Combination formula.

I have included both the formulas in this Google Sheets tutorial, but I prefer the Vlookup over SUBSTITUTE.

Though both have their plus and minuses, Vlookup is better as it can easily handle a large set of data.

Update on 03-12-2021:- Other than the above formula options 1 and 2, I have included one more formula (option 3), which is more dynamic.

Where is this going to work?

If you are looking for a way to replace comma-separated product IDs with matching product names or vice versa, you can follow these tips. 

No doubt, in any similar scenarios, you can use any of the below formulas.

Replace Multiple Comma Separated Values in Google Sheets

In the above example, I have multiple comma-separated values in column A, which are product IDs.

We can match (lookup) these product IDs with the equivalent/matching products with the help of the table in the range D4:E8.

For this, we can use a formula which you can find below. The formula output is in column B.

How to Replace Multiple Comma Separated Values in Google Sheets

First, let’s use the SUBSTITUTE function to replace comma-separated values in Google Sheets as above. Here is that formula.

Option 1: Nested SUBSTITUTE Formula

Make sure that the cells in the range B2:B are blank. Use the below formula in cell B2.

=ArrayFormula(
     SUBSTITUTE(
        SUBSTITUTE(
           SUBSTITUTE(
              SUBSTITUTE(
                 A2:A,"IDA1001","Apple"
              ),"IDM2002","Mango"
           ),"IDO2001","Orange"
        ),"IDB1111","Banana"
     )
)

It’s the same above formula. But, here, the IDs and Products are replaced by cell references in D5:E8.

=ArrayFormula(
     SUBSTITUTE(
        SUBSTITUTE(
           SUBSTITUTE(
              SUBSTITUTE(
                 A2:A,D5,E5
              ),D6,E6
           ),D7,E7
        ),D8,E8
     )
)

Use any of the above formulas to replace multiple comma-separated values in Google Sheets.

Pros:-

  • It’s an array formula that expands.

Cons:-

  • If the number of replacement texts is large, there are chances of typos. Also, it would be a hefty task to build the formula. In short, the range A2:A is open, but the lookup table D5:E8 is closed.

Option 2: Vlookup Formula to Replace Multiple Comma Separated Values

Here I am not using the REPLACEREGEXREPLACE, or SUBSTITUTE text replacement functions! Then?

Instead, you can see a lookup and replace approach below.

You can use this Vlookup formula to replace multiple comma-separated values in Google Sheets.

I am recommending this formula since it can handle a large number of IDs and Products without any modification.

=ArrayFormula(
     textjoin(
        ", ",true,
        ifna(
           vlookup(
              TRANSPOSE(trim(split(A2,","))),
              {$D$5:$D&"",$E$5:$E},2,0
           )
        )
     )
)

How to use this Vlookup to substitute multiple Comma-separated values?

Update:-

I have slightly modified this formula. Replaced JOIN with TEXTJOIN and included an IFNA additionally. So you may find a different one on the screenshot below.

Vlookup to Replace Multiple Comma Separated Values

Apply the formula in cell B2 and drag it down.

Pros:-

  • Supports an ‘unlimited’ number of replacement values (D5:E) without touching the formula. In other words, the lookup table D5:E8 is open.

Cons:-

  • It is not providing an expanded array result. You may copy-paste the formula to apply in multiple rows. I mean, in the above example, you need to insert this Vlookup formula in cell B2 and then drag it down.
  • The formula will replace mismatching values with blank.
  • In cell A4, if the value is “IDB1, IDB1111, IDB2, IDA1001,” the formula will return “Banana, Apple.”

Option 3: Dynamic Array Formula to Replace Multiple Comma Separated Values

A couple of months back, Google has officially introduced the function FLATTEN in Sheets.

That made me possible to code another formula with two open ranges.

Here both A2:A and D5:D are open. So let me call it a dynamic formula to replace multiple comma-separated values in Google Sheets.

Insert the below dynamic range formula in cell B2. It will expand down.

=ArrayFormula(
     regexreplace(
        flatten(trim(split(textjoin(" ",false,
           ifna(
              vlookup(
                 flatten(split(substitute(filter(A2:A,A2:A<>"")&" |",",","")," ")),
                 D5:E,2,0
              ),
              flatten(split(substitute(filter(A2:A,A2:A<>"")&" |",",","")," "))
           )
        ),"|"))),
        "\s", ", "
     )
)

Pros:-

  • Similar to option # 1, it’s an array formula. So it expands down without copy-paste.
  • It addresses one issue that we face with the option # 1 formula. There only the range A2:A is open. But here, I have specified infinite or opened ranges in the lookup table (D5:E) and search keys (A2:A).
  • It addresses two issues with the option # 2 formula.
    • There we have used a non-array (copy-paste) formula. Here we have an array formula.
    • There the formula replaces mismatching values with blank. Here, on the contrary, the formula will leave the mismatching values as it is.

Cons:-

  • You may not leave any blank cells between the values in the range A2:A.

That’s all. This way, you can replace multiple strings in a cell/column in Google Sheets.

Example Sheet 031221

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

More like this

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

6 COMMENTS

  1. Hello Prashanth,

    As usual, your creativity with formulas surprises me every time!

    I arrived on this page because I need to replace the content on a cell in a similar way to this one. At least so I thought before trying! =)

    Instead of a product list separated by a comma, I have a sentence like:

    There is IDM2002, and we also have IDM2001 and IDM2003.

    I would like it to become:

    There is Mango, and we also have Apple and Orange.

    I’m looking for formula because my substitution list is close to 1000 elements, and it will be growing over time.

    As far as you know, is there a way to have this behavior within a cell in Google Sheets?

    Kind regards,

    Matteo

    • Hi, Matteo,

      The same formulas in my tutorial would work. If you face issues, make an example in a new sheet and share that sheet with me via a reply to this thread. I won’t publish your reply with the Sheet link.

      • Hello Prashanth.

        Thanks for the quick reply! I set up a sheet to play with. And I played, but it seems trickier than expected… =/

        I’m looking forward to your opinion! =)

        Thanks again,

        Matteo

        • Hi, Matteo,

          I have tried my level best and could solve the problem partly. I mean I could replace multiple words in a text at the cost of losing some special characters like the dot, comma, exclamation, and the new line.

          You may please find the same in the new tab named “kvp” in your file.

          • Prashanth, that’s a very wonderful formula you wrote there.
            And I can now say that—if even you couldn’t find a way to have everything working—it means the issue is simply not solvable through GS.
            I learned a lot!

            Thank you very much for trying, I really appreciated it!

            Matteo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.