473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

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

MMcCarthy
14,534 Expert Mod 8TB
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, 7558 views)
File Type: jpg ValuesSearch.jpg (70.4 KB, 3268 views)
File Type: jpg FormulasSubstitute.jpg (75.1 KB, 2951 views)
File Type: jpg ValuesSubstitute.jpg (76.1 KB, 2963 views)
File Type: jpg FormulasReplace.jpg (81.3 KB, 3463 views)
File Type: jpg ValuesReplace.jpg (76.6 KB, 2582 views)
May 20 '13 #1
0 8377

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: harshan | last post by:
Hi I am trying to read required worksheet from excel file using perl with module spreadsheet::parseexcel and spreadsheet::writeexcel but I am unable to read it, the error I am getting is...
1
by: Zenobia | last post by:
Hello I want a search & replace text in source code for several files in several directories. It would seem that both Dreamweaver MX 6 and GoLive 6 offer this feature but not for .aspx file...
10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
3
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
2
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
3
by: Rene | last post by:
Hi Proggies, i try to delete an excel worksheet but nothing happens (work on vb.net 2005) ..... xlSheet = xlApp.Sheets("Master") xlSheet.select xlSheet.delete also try
6
by: DataSmash | last post by:
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! #...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.