472,793 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,793 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, 7499 views)
File Type: jpg ValuesSearch.jpg (70.4 KB, 3241 views)
File Type: jpg FormulasSubstitute.jpg (75.1 KB, 2930 views)
File Type: jpg ValuesSubstitute.jpg (76.1 KB, 2928 views)
File Type: jpg FormulasReplace.jpg (81.3 KB, 3438 views)
File Type: jpg ValuesReplace.jpg (76.6 KB, 2562 views)
May 20 '13 #1
0 8183

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.