The SUBSTITUTE Function replaces specific text within a piece of text replacing all instances unless otherwise specified. A key function used to clean your data of unwanted characters.
The SUBSTITUTE function is a computer program that you run from a worksheet cell formula. It replaces specific text within a piece of text replacing all instances unless otherwise specified. You run the SUBSTITUTE function by typing its name in a formula then followed by the information it is suppose to substitute. The SUBSTITUTE worksheet function is generally used when you either want to replace characters in a piece of text or get rid of unwanted characters in a piece of text. For example, suppose you get a table from a database download and discover that the code x1 has been embedded at the beginning of each sales code. These extra characters hider your LOOKUP functions like VLOOKUP. You could use the SUBSTITUTE function to get rid of the x1. Remember SUBSTITUTE replaces specified instances of the text occurrence so to replace a specific instance of a piece of text at a certain character position use the REPLACE function instead.
Whenever you type a formula in a worksheet cell, this is called syntax or grammar. The general SUBSTITUTE function syntax has a format like this when you type it in a worksheet cell:
=SUBSTITUTE(text, old_text, new_text, [instance_num])
Where text, old_text, new_text, [instance_num] ..... are called the function argument list. Remember, you are running a computer program at this point so the program needs information to operate and that is why there is an argument list. When you see an argument list and you see square brackets [ ] around the argument name, this means the argument is optional and you do not have to include it when typing unless you need it. So for the syntax above, you need to include three arguments for the SUBSTITUTE function when typing it in a worksheet cell formula in order for it to calculate correctly. What argument values can be used are discussed below. Remember functions expect certain things in their argument lists, if you do not put the correct information in the list they will generate an error when run.
Since the SUBSTITUTE function is a computer program, it runs when you press Enter to enter the formula that contains it. If any of the arguments are wrong, the function will return an error.
When typing the SUBSTITUTE function in a worksheet cell formula, you need to replace the argument list with arguments separating each one with a comma (,). Some typical arguments you can use are:
|Argument Type||Cell Formula||Example Explanation|
|Cell Reference||= SUBSTITUTE( A1, "x1","" )||SUBSTITUTE deletes the text x1 in the text from cell A1 replacing every instance of it|
|Nested Functions||= SUBSTITUTE( A1:A10, "x1","" )||SUBSTITUTE deletes the text x1 in the text from the range A1 to A10 replacing every instance of it and returning an array|
|Cell and Range Names||= SUBSTITUTE( Code_Name," ","" )||SUBSTITUTE deletes all spaces from the text in the cell referenced by the Code_Name name *|
* It is possible to name a cell or group of cells on a worksheet and use that name in place of a range reference or cell reference. Consult Excel help on how to name a cell.
The worksheet seen below contains some typical worksheet formulas that run the SUBSTITUTE worksheet function. Pay close attention to the argument list and the syntax used to write the formula.
|1||x1A001||= SUBSTITUTE(A1, "x1","")||Returns A001 from the text from cell A1|
|2||CAA 567 AA||= SUBSTITUTE(A2," ","")||Returns CAA567AA from the text from cell A2 removing all spaces|
|1234CA||= SUBSTITUTE( A3, CHAR(10), "*" )||Finds the character returned by the CHAR function number 10 which is a line feed and replaces it with *. The CHAR function is great for going after characters that you cannot type. It takes the ASCII code decimal value as an argument.|