The tutorial explains the Excel REPLACE and SUBSTITUTE functions with examples of uses. See how to use the REPLACE function with text strings, numbers and dates, and how to nest several REPLACE or SUBSTITUTE functions within one formula.
Last week we discussed various ways of using FIND and SEARCH functions within your Excel worksheets. Today, we will be taking a deeper look at two other functions to replace text in a cell based on its location or substitute one text string with another based on content. As you may have guessed, I am talking about the Excel REPLACE and SUBSTITUTE functions.
Excel REPLACE function
The REPLACE function in Excel allows you to swap one or several characters in a text string with another character or a set of characters.
As you see, the Excel REPLACE function has 4 arguments, all of which are required.
- Old_text - the original text (or a reference to a cell with the original text) in which you want to replace some characters.
- Start_num - the position of the first character within old_text that you want to replace.
- Num_chars - the number of characters you want to replace.
- New_text - the replacement text.
For example, to change the word "sun" to "son", you can use the following formula:
=REPLACE("sun", 2, 1, "o")
And if you put the original word in some cell, say A2, you can supply the corresponding cell reference in the old_text argument:
=REPLACE(A2, 2, 1, "o")
Note. If the start_num or num_chars argument is negative or non-numeric, an Excel Replace formula returns the #VALUE! error.
Using Excel REPLACE function with numeric values
The REPLACE function in Excel is designed to work with text strings. Of course, you can use it to replace numeric characters that are part of a text string, for example:
=REPLACE(A2, 7, 4, "2016")
Notice that we enclose "2016" in double quotes as you usually do with text values.
In a similar manner, you can replace one or more digits within a number. For example:
=REPLACE(A4, 4, 4,"6")
And again, you have to enclose the replacement value in double quotes ("6").
Note. An Excel REPLACE formula always returns a text string, not number. In the screenshot above, notice the left alignment of the returned text value in B2, and compare it to the right-aligned original number in A2. And because it's a text value you won't be able to use it in other calculations unless you convert it back to number, for example by multiplying by 1 or by using any other method described in How to convert text to number.
Using Excel REPLACE function with dates
As you have just seen, the REPLACE function works fine with numbers, except that it returns a text string :) Remembering that in the internal Excel system, dates are stored as numbers, you may try to use some Replace formulas on dates. Results would be quite embarrassing.
For instance, you have a date in A2, say 1-Oct-14, and you want to change "Oct" to "Nov". So, you write the formula REPLACE(A2, 4, 3, "Nov") that tells Excel to replace 3 chars in cells A2 beginning with the 4th char… and got the following result:
Why's that? Because "01-Oct-14" is only a visual representation of the underlying serial number (41913) that represents the date. So, our Replace formula changes the last 3 digits in the above serial number to "Nov" and returns the text string "419Nov".
To get the Excel REPLACE function to correctly work with dates, you can convert dates to text strings first by using the TEXT function or any other technique demonstrated in How to convert date to text in Excel. Alternatively, you can embed the TEXT function directly in the old_text argument of the REPLACE function:
=REPLACE(TEXT(A2, "dd-mmm-yy"), 4, 3, "Nov")
Please remember that the result of the above formula is a text string, and therefore this solution works only if you are not planning to use the modified dates in further calculations. If you do need dates rather than text strings, use the DATEVALUE function to turn the values returned by the Excel REPLACE function back to dates:
=DATEVALUE(REPLACE(TEXT(A2, "dd-mmm-yy"), 4, 3, "Nov"))
Nested REPLACE functions to do multiple replacements in a cell
Quite often, you may need to do more than one replacement in the same cell. Of course, you could do one replacement, output an intermediate result into an additional column, and then use the REPLACE function again. However, a better and more professional way is to use nested REPLACE functions that let you perform several replacements with a single formula. In this context, "nesting" means placing one function within another.
Consider the following example. Supposing you have a list of telephone numbers in column A formatted as "123456789" and you want to make them look more like phone numbers by adding hyphens. In other words, your goal is to turn "123456789" into "123-456-789".
Inserting the first hyphen is easy. You write a usual Excel Replace formula that replaces zero characters with a hyphen, i.e. adds a hyphen in the 4th position in a cell:
=REPLACE(A2,4,0,"-")
The result of the above Replace formula is as follows:
Okay, and now we need to insert one more hyphen in the 8th position. To do this, you place the above formula within another Excel REPLACE function. More precisely, you embed it in the old_text argument of the other function, so that the second REPLACE function will handle the value returned by the first REPLACE, and not the value in cell A2:
=REPLACE(REPLACE(A2,4,0,"-"),8,0,"-")
As the result, you get the phone numbers in the desired formatting:
In a similar manner, you can use nested REPLACE functions to make text strings look like dates by adding a forward slash (/) where appropriate:
=(REPLACE(REPLACE(A2,3,0,"/"),6,0,"/"))
Moreover, you can convert text strings into real dates by wrapping the above REPLACE formula with the DATEVALUE function:
=DATEVALUE(REPLACE(REPLACE(A2,3,0,"/"),6,0,"/"))
And naturally, you are not limited in the number of functions you can nest within one formula (the modern versions of Excel 2010, 2013 and 2016 allow up to 8192 characters and up to 64 nested functions in a formula).
For example, you can use 3 nested REPLACE functions to have a number in A2 appear like date and time:
=REPLACE(REPLACE(REPLACE(REPLACE(A2,3,0,"/") ,6,0,"/"), 9,0, " "), 12,0, ":")
Replacing a string that appears in a different position in each cell
So far, in all the examples we have been dealing with values of a similar nature and have made replacements in the same position in each cell. But real-life tasks are often more complicated than that. In your worksheets, the characters to be replaced may not necessarily appear in the same place in each cell, and therefore you will have to find the position of the first character that should be replaced. The following example will demonstrate what I'm talking about.
Supposing you have a list of email addressing in column A. And the name of one company has changed from "ABC" to, say, "BCA". So, you have to update all of the clients' email addressing accordingly.
But the problem is that the client names are of different length, and that is why you cannot specify exactly where the company name begins. In other words, you do not know what value to supply in the start_num argument of the Excel REPLACE function. To find it out, use the Excel FIND function to determine the position of the first char in the string "@abc":
=FIND("@abc",A2)
And then, supply the above FIND function in the start_num argument of your REPLACE formula:
=REPLACE(A2, FIND("@abc",A2), 4, "@bca")
Tip. We include "@" in our Excel Find and Replace formula to avoid accidental replacements in the name part of email addresses. Of course, there's a very slim chance that such matches will occur, and still you may want to be on the safe side.
As you see in the following screenshot, the formula has no problem with finding and replacing the old text with the new one. However, if the text string to be replaced is not found, the formula returns the #VALUE! error:
And we want the formula to return the original email address instead of the error. So, let's enclose our FIND & REPLACE formula in the IFERROR function:
=IFERROR(REPLACE(A2, FIND("@abc",A2), 4, "@bca"),A2)
And this improved formula works perfectly, doesn't it?
Another practical application of the REPLACE function is to capitalize the first letter in a cell. Whenever you deal with a list of names, products, and the like, you can use the above-linked formula to change the first letter to UPPERCASE.
Tip. If you want to make the replacements in the original data, an easier way would be using the Excel FIND and REPLACE dialog.
Excel SUBSTITUTE function
The SUBSTITUTE function in Excel replaces one or more instances of a given character or text string with a specified character(s).
The syntax of the Excel SUBSTITUTE function is as follows:
The first three arguments are required and the last one is optional.
- Text - the original text in which you want to substitute characters. Can be supplied as a test string, cell reference, or a result of another formula.
- Old_text - the character(s) you want to replace.
- New_text - the new character(s) to replace old_text with.
- Instance_num - the occurrence of old_text you want to replace. If omitted, every occurrence of the old text will be changed to the new text.
For example, all of the below formulas substitute "1" with "2" in cell A2, but return different results depending on which number you supply in the last argument:
=SUBSTITUTE(A2, "1", "2", 1)
- Substitutes the first occurrence of "1" with "2".
=SUBSTITUTE(A2, "1", "2", 2)
- Substitutes the second occurrence of "1" with "2".
=SUBSTITUTE(A2, "1", "2")
- Substitutes all occurrences of "1" with "2".
In practice, the SUBSTITUTE function is also used for removing unwanted characters from cells. For real-life examples, please see:
Note. The SUBSTITUTE function in Excel is case-sensitive. For example, the following formula replaces all instances of the uppercase "X" with "Y" in cell A2, but it won't replace any instances of the lowercase "x".
Substitute multiple values with a single formula (nested SUBSTITUTE)
As is the case with the Excel REPLACE function, you can nest several SUBSTITUTE functions within a single formula to do several substitutions at a time, i.e. substitute several characters or substrings with a single formula.
Supposing you have a text string like "PR1, ML1, T1" in cell A2, where "PR" stands for "Project, "ML" stands for "Milestone" and "T" means "Task". What you want is to replace the three codes with full names. To achieve this, you can write 3 different SUBSTITUTE formulas:
=SUBSTITUTE(A2,"PR", "Project ")
=SUBSTITUTE(A2, "ML", "Milestone ")
=SUBSTITUTE(A2, "T", "Task ")
And then nest them into each other:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"PR","Project "),"ML","Milestone "),"T","Task ")
Notice that we've added a space at the end of each new_text argument for better readability.
To learn other ways to replace multiple values at a time, please see How to do mass find and replace in Excel.
Excel REPLACE vs. Excel SUBSTITUTE
The Excel REPLACE and SUBSTITUTE functions are very similar to each other in that both are designed to swap text strings. The differences between the two functions are as follows:
- SUBSTITUTE replaces one or more instances of a given character or a text string. So, if you know the text to be replaced, use the Excel SUBSTITUTE function.
- REPLACE changes characters in a specified position of a text string. So, if you know the position of the character(s) to be replaced, use the Excel REPLACE function.
- The SUBSTITUTE function in Excel allows adding an optional parameter (instance_num) that specifies which occurrence of old_text should be changed to new_text.
This is how you use the SUBSTITUTE and REPLACE functions in Excel. Hopefully, these examples will prove useful in solving your tasks. I thank you for reading and hope to see on our blog next week!
Download practice workbook
REPLACE and SUBSTITUTE formula examples (.xlsx file)
301 comments
exmple
row c5 2020 41566565
if found in row c5 such cell change in r5521
its cell complete change
how can change data such type
Hi!
I am not sure I fully understand what you mean.
Hi, very nice thing you got here going on.
I've encountered a little problem with the substitute function. Assuming this is the formula I use in my reference cell S2:
=SUM(IF(AY2=AZ2;1;0)+IF(AY3=AZ3;1;0)
And then I use in another cell:
=SUBSTITUTE(SUBSTITUTE(S2;"AY";"BG"),"AZ","BH")
to keep the formula the same, but to have every AY substituted by BG, and every AZ by BH. However, I can't get it to work. How come?
Thank you in advance.
BTW I did notice that in your examples, in the examples, ', ' (a comma) is used, whereas I use ';' (a point comma). But the point comma is automatically used by the excel I use itself, so that can't be the issue I guess.
Hello!
You cannot change the formula with another formula. You can use the INDIRECT function to create links.
I have a conversion table. I would like Excel to automatically change the numbers when I type them in.
For example;
if I type in 176123456789, I'd like it to change the numbers to 176xxxxx6789
A part of the conversion table looks like this:
176xxxxx6789 after i type the number inside
and it continues. is this a possibility?
If your number length is fixed then you can use below formula.
=SUBSTITUTE(A2,MID(A2,4,5),"xxxx")
I have a conversion table. I would like Excel to automatically change the numbers when I type them in.
For example;
if I type in 10.25, I'd like it to change the numbers after the decimal point to .4... final number should look like 10.4
A part of the conversion table looks like this:
.10-.15=.2 (.10 THROUGH .15 is .2)
.16-.21=.3
.22-.27=.4
and it continues. is this a possibility?
Thank you for your consideration.
Hi!
You cannot change the number that you wrote in a cell using an Excel function. You need to use VBA macro. In the adjacent cell, you can use the IF function to get the number according to your conditions.
I hope I answered your question. If you have any other questions, please don’t hesitate to ask.
i want to enter numbers that will be translated in text (pre-defined for each number)
for example , if i enter "0" in a cell and press enter, it will be translated as "negative"
1 = positive
2 = good ,etc
Hi!
Use a VBA macro. But you cannot enter ordinary numbers.
Hi ATTA:
You can use the following formula:
=IF(F5=0, "Negative", IF(F5=1, "Positive", IF(F5=2, "Good", "False")))
Hello,
How i can replace below .
11.11111.1111 to 11.11111-1111
the last dot should be dash.
Best regards
Hello!
You can use this formula:
=SUBSTITUTE(A1,".","-",LEN(A1)-LEN(SUBSTITUTE(A1,".","")))
I recommend reading this guide: How to count characters in Excel
Hi,
Sir,
Thank you very much.
Best regards
Hi Shajith,
You can also use the following formula:
=REPLACE(B20, 9, 1, "-")
I used "SUBSTITUTE" function to remove commas from a number but now I can't add the number I got with other numbers since it is now stored as a text I guess. Any solution for this??
Hi!
If you are talking about a decimal point, then use the rounding functions. If this is not what you want, please explain in more detail.
Give an example of the source data and the expected result.
For some reason, when I try to use the SUBSTITUTE() function in VBA code, I get an error that says "Sub or Function not defined" when running the macro, with the "Substitute" portion of the code highlighted, as if VBA can't find that function. Am I missing a reference package or something? I thought Substitute() was part of the base Excel install. Why isn't VBA find that function??? Replace() it can find, and everything else I've tried, just not Substitute().
BTW, if I type it directly into a cell as a formula, it works OK there. Just not in VBA for some reason.
Is there any possibility of adding specific texts to allocated number values please?
e.g.
0 = None
1 = Tea
2 = Cocoa
3 = Coffee
Thank you in advance for any suggestions.
Hi!
Based on your description, it is hard to completely understand your task. However, I’ll try to guess and offer you the following formula:
=CHOOSE(B1+1,B1&"None", B1&"Tea", B1&"Cocoa", B1&"Coffee")
If this is not what you wanted, please describe the problem in more detail.
Is there any possibility of adding specific texts to allocated number values please?
e.g.
0 = Not Yet Achieved
1 = Pass
2 = Merit
3 = Distinction
Thank you in advance for any suggestions.
hi Cam,
You can use the following formula:
=IF(F9=0, "Not Yet Achieved", IF(F9=1, "Pass", IF(F9=2, "Merit", IF(F9=3, "Distinction", "False"))))
where, F9 is the first cell number with the number 0.
how can I change this format to 04-05-2021 AM 2:45:55 to 04-05-2021 2:45:55 AM
Hello!
If your values are written as date, then apply a custom date format as described in this article.
If your date is written as text, use the formula
=REPLACE(LEFT(A1,LEN(A1)-8),SEARCH(" ",A1,1),1, RIGHT(A1,LEN(A1)-13)&" ")
I hope I answered your question. If something is still unclear, please feel free to ask.
The string is an NCAA basketball tournament game schedule as follows:
6:30 pm — No. 16 N.C. Central vs. No. 16 Texas Southern (West/Dayton, OH), TruTV
Where each team name can be one, two or three words as follows:
4:00 pm — No. 5 Ohio State vs. No. 12 South Dakota State (West/Boise, ID), TNT
Have used substitute to replace all unnecessary characters so that desired data elements are all separated by a single space...
6:30 16 N.C. Central 16 Texas Southern West Dayton OH TruTV
4:00 5 Ohio State 12 South Dakota State West Boise ID TNT
Each space separated element (other than team name) needs to become its own field in the record. Team name is a separate field. If the pm time could be appropriately valued numerically, that would be a bonus for sure.
Thank you for any suggestions.
Yours is an excellent site. I have no problem purchasing a license for your tool set, but I'm not sure if the mechanics are there to execute this very specific string extract problem...
Hi Sam,
You can use our Split Text tool to split your strings by this mask:
* — No. * * vs. No. * * (*), *
The following image shows the settings and outcomes:
https://cdn.ablebits.com/_img-blog/_comments/comment-640523.png
Also, you can download a trial version using this link and test it on real data. Perhaps, you will need to additionally remove some unwanted characters by using the Remove tool.
If you have any further questions or concerns, just let me know :)
Hi,
its possible to rename needed phraze from Cell1 to needed phraze from Cell2?
for example -->
=SUBSTITUTE(A1;"cell1 phraze";("phraze from different cell2"))
is there option to read information from other cell?
Hi,
If "Cell1" is cell A1, then this is not possible. If A1 says "cell1 phraze" then where do you want to write the formula that will change this phrase?
If this is not what you wanted, please describe the problem in more detail.
I need to copy a delimited cell into another cell, but only the values between the delimiters that are less than 30 characters long.
E.g.
Reference cell: blue||green||Adding another tag||this tag is toooooooooooooooooooooooo long||this tag is good||short tag
I want to extract all of the tags that are under 30 characters and put them in the next cell so it would be:
blue||green||Adding another tag||this tag is good||short tag
I know how to do this from a column, but need to figure out how to do it from a delimited list and am having no luck.
Hello!
You need to split the text into cells and then select the ones you want. I recommend paying attention to the tool Split Text. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free. If something is still unclear, please feel free to ask.
=SUBSTITUTE(A2,"PR", "Project ")
Instead of substituting PR with “Project” how would you substitute PR with the content of a cell ?
Example content of A9 = “Happy”
Result= PR becomes Happy
Need to do this with a list of names that will have their own unique URL
Hello!
If I got you right, the formula below will help you with your task:
=SUBSTITUTE(A2,"PR", A9)
If this is not what you wanted, please describe the problem in more detail.
How can I substitute max to AJanMAX
How can i convert " 19:00-20:00-UAE " to " 1900 - 2000" format.
I'm cleaning a large list of addresses with all different formatting. Some were in all Caps (so I cleaned that by using the Proper formula), some spell out Road vs Rd. vs Rd (no period). I'm using Find and Replace, however, I'm running into issues. When trying to find all the addresses using 'Road' and Replace with 'Rd', it will also replace Roadway to Rdway which I do not want. Another example is Finding all the 'Se' (for South East) to Replace as 'SE', excel finds all words that start with Se such as 'Second' and will replace it with 'SEcond'. Is there a way to search for just a two-letter word (ie; Se, Ne, Nw, etc) so I can replace just what I want? Or just look for the word Road and not Roadway? Any advice will be helpful. Thank you
Hello!
You can search for the desired word in 3 steps. To find an exact match for a word, add a space after the word in the search box. Then repeat your search and add a space before the word. After that, search for the word and use the "Match entire cell contents" option.
I hope my advice will help you solve your task.
Thank you. Your advice has been very helpful.
Hello,
I am looking for a formula that will convert seconds into weeks, days, hours, minutes and a way to only show these if the value is not 0. So for example, 13500seconds would convert to 3h 45m.
Thank you!
Hello!
To convert seconds to time, divide by 86400.
You can learn more about convert time in Excel in this article on our blog.
Excel has empty commas in one column e.g. ,, OR ,,, OR ,,,, they are not same number but all are empty.
We want to add text to other columns e.g. "A" "B" "C" etc.
We want to have a substitute formula that will replace all spaces in column one with text available in other columns.
So if there are two ,, we want to replace with A,B,C
if there are three ,,, we want to replace with A, B, C, D
if there is one then replace with A,B
how can we do this in excel? Thanks in advance!
Hello!
If I got you right, the formula below will help you with your task:
=LEFT("ABCDEFGHIJKLMNOPQRSTUVWXYZ",LEN(A1)+1)
But if you want to replace commas with letters in the first column, then this is only possible with a VBA macro.