How to Replace Last Comma in String with “and” word

This post will guide you how to replace last comma character in a string in cells with “and” word in excel. How do I replace the last comma character from a string with another word “and” using an excel formula. How to replace last instance of comma with ‘and’ in a string in excel.

1. Replace Last Comma in String with “and”

If you have a list of data (B1:B4) that contain the text string and each word is separated by comma character. And you want to replace the last comma character with a new word “and”. How to achieve it.

You need to create a new complex formula based on the SUBSTITUTE function, and the LEN function to achieve the result. Just do the following steps:

#1 select one blank cell and type the following formula into the formula box.

=SUBSTITUTE(B1,","," and",LEN(B1)-LEN(SUBSTITUTE(B1,",","")))
replace last comma1

#2 then press Enter key in your keyboard and drag the AutoFill Handler over other cell to apply this formula.

You will that all last commas in each selected cells have been replaced with “and” word.

replace last comma2

2. Replace Last Comma in String with “and” word Using VBA

Now, let’s explore the second method, which involves a user-defined function using VBA code. Follow these steps to run the VBA code:

Step1: Press “Alt + F11” to open the VBA editor.

Step2: In the VBA editor, click on “Insert” and select “Module” to create a new module.

Step3: Copy the provided VBA code and paste it into the module. Close the VBA editor. Once saved, you can use the new function directly in your worksheet.

How to Replace Last Comma in String with “and” word
Function ReplaceLastCommaWithAnd(inputString As String) As String
    Dim lastCommaPos As Integer
    
    ' Find the position of the last comma in the string
    lastCommaPos = InStrRev(inputString, ",")
    
    ' Check if a comma is found in the string
    If lastCommaPos > 0 Then
        ' Replace the last comma with 'and'
        ReplaceLastCommaWithAnd = Left(inputString, lastCommaPos - 1) & " and" & Mid(inputString, lastCommaPos + 1)
    Else
        ' If no comma is found, return the original string
        ReplaceLastCommaWithAnd = inputString
    End If
End Function

Step4: Now, you can use the function ReplaceLastCommaWithAnd in your Excel worksheet.

 For example, if your string is in cell A1, you can enter the formula in another cell to get the modified string.

=ReplaceLastCommaWithAnd(A1)
How to Replace Last Comma in String with “and” word

This method adds a personalized touch to your Excel experience, providing flexibility in your string manipulation tasks.

3. Video: Replace Last Comma in String with “and” word

This Excel video tutorial where we explore two powerful methods to transform strings by replacing the last comma with the word ‘and’. Let’s dive into the first method, using a formula, and then explore the second method employing a user-defined function in VBA.

4. Related Functions

  • Excel Substitute function
    The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string.The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE  (text, old_text, new_text,[instance_num])….
  • Excel LEN function
    The Excel LEN function returns the length of a text string (the number of characters in a text string).The syntax of the LEN function is as below:= LEN(text)…

Leave a Reply