423,846 Members | 2,048 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to use the SEARCH, SUBSTITUTE and REPLACE Worksheet Functions in Excel

MMcCarthy
Expert Mod 10K+
P: 14,534
There are so many functions available in Excel Worksheets that users often stick with the most common ones until they start exploring trying to find a solution to a problem. Sometimes it doesn't even occur to them that there may be a function to deal with the issue. So I decided to draw on my experience and the experience of some of my colleagues to note some of the lessor known functions we have found to be very useful.

As the title of this article suggests these are Excel Worksheet functions for use in cell formula as opposed to VBA functions. Although some may also be available in VBA they are not as a rule. Where the function is available in VBA I will state it.

SEARCH()
The SEARCH function is used to "search" text for a given substring. It is not case sensitive. If you need something that is case sensitive then use the SUBSTITUTE function instead. The SEARCH function can be very useful if you need to search a column of text for a particular reference to something.

For example, if you have a column of addresses and wish to search for all those addresses in a particular town, then you can search for the town as a substring of the text in the address column.

The syntax for the function is:
SEARCH (substring, Cell Reference of Text, [start_position] )
Note: start_position is optional. If no value is entered it defaults to 1, the first character of the Body of Text.

In this first image you can see the formulas I used to test the function. In Cell Reference A1 I inserted a block of text (In this case an address). In Cell Reference A2 I entered the SEARCH Function and inserted the substring I want to search for and passed it the Value in A1 to search.



In the below image you can see the values when I turn off the Formula display. You will note that it returns the position of the text when it is found (which can be useful) and #VALUE error if the substring is not found. If you need to return a value such as 0 if the substring is not found you can use the ISERROR function to evaluate the result.




SUBSTITUTE()
The SUBSTITUTE function is used to do substitute one set of characters for another. There are many real world examples where this would be useful but I am using a simple one to explain.

The syntax for the function is:
SUBSTITUTE ( cell reference, oldText, newText, [nth time oldText appears] )
Note: [nth time oldText appears] is optional. It allows you to designate only replacing a specific appearance of the old text. If no value is entered for this all appearances of the old text will be substituted with the new text.

In this first image you can see the formulas I used to test the function. The text I am evaluating is in column A. In this case I am telling it what position in the text the characters I wish to replace appear and how many characters to remove from the text before inserting the new text.



In the below image you can see the values when I turn off the Formula display.




REPLACE() ... VBA has a similar function but it has different syntax
The REPLACE function replaces a sequence of characters in a string with another set of characters. Unlike the SUBSTITUTE function you are not giving an old set of characters to replace but rather the position (start position) in a block of text and the number of characters in the block you wish to replace.

The syntax for the function is:
REPLACE ( Cell Reference, Start_position, number of characters, newText )

In this first image you can see the formulas I used to test the function. The text I am evaluating is in column A.



In the below image you can see the values when I turn off the Formula display.




Mary McCarthy
Bytes Admin
Attached Images
File Type: jpg FormulasSearch.jpg (76.9 KB, 3806 views)
File Type: jpg ValuesSearch.jpg (70.4 KB, 2599 views)
File Type: jpg FormulasSubstitute.jpg (75.1 KB, 2281 views)
File Type: jpg ValuesSubstitute.jpg (76.1 KB, 2353 views)
File Type: jpg FormulasReplace.jpg (81.3 KB, 2864 views)
File Type: jpg ValuesReplace.jpg (76.6 KB, 2133 views)
May 20 '13 #1
Share this Article
Share on Google+