excel text and string functions_ left, right, mid, len, find, search, replace, s

9
Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM] Email Id Password Excel Tips Excel Functions Excel VBA Excel Pivot Tables Free! Excel VBA Online Tutorial Remember Me Forgot your password? Create an account You are here: Excel Functions Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, EXCEL FUNCTIONS Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE User Rating: / 48 Poor Best Details Excel Text and String Functions: Excel Functions for Finding and Replacing Text, with Examples: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE Related Links: 1. Excel Text and String Functions: TRIM & CLEAN. 2. Excel CODE & CHAR functions. ------------------------------------------------------------------------------------------------------------- ------------------------------------------------- This section deals with the Excel Worksheet String Functions - click here for detailed explanation of Excel VBA String Functions. LEFT Function (Worksheet / VBA) The Excel LEFT function can be used both as a worksheet function and a VBA function. The LEFT function returns the specified number of characters in a text string, starting from the first or left-most character. Use this function to extract a sub-string from the left part of a text string. Syntax: LEFT(text_string, char_numbers). It is necessary to mention the text_string argument which is the text string from which you want to extract the specified number of characters. The char_numbers argument is optional (when using as a worksheet function), which specifies the number of characters to extract from the text string. The char_numbers value should be equal to or greater than zero; if it is greater than the Excel and VBA Solutions Excel & VBA VBA Tutorial Login HOME EXCEL VBA ONLINE TUTORIAL EXCEL MODELS SERVICES FEEDBACK

Upload: charzree

Post on 15-Dec-2015

1 views

Category:

Documents


0 download

DESCRIPTION

Excel Training- Text category formulas

TRANSCRIPT

Page 1: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

Email Id

Password

Excel Tips

Excel Functions

Excel VBA

Excel Pivot Tables

Free! Excel VBAOnline Tutorial

Remember Me

Forgot yourpassword?Create anaccount

You are here: Excel Functions Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE,

EXCEL FUNCTIONS

Excel Text and String Functions: LEFT, RIGHT, MID,LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

User Rating: / 48 Poor Best

D e t a i l s Excel Text and String Functions:

Excel Functions for Finding and Replacing Text, with Examples: LEFT,RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE Related Links:1. Excel Text and String Functions: TRIM & CLEAN.2. Excel CODE & CHAR functions. -------------------------------------------------------------------------------------------------------------------------------------------------------------- This section deals with the Excel Worksheet String Functions - click here for detailedexplanation of Excel VBA String Functions. LEFT Function (Worksheet / VBA) The Excel LEFT function can be used both as a worksheet function and a VBA function.The LEFT function returns the specified number of characters in a text string, starting fromthe first or left-most character. Use this function to extract a sub-string from the left part of atext string. Syntax: LEFT(text_string, char_numbers). It is necessary to mention thetext_string argument which is the text string from which you want to extract the specifiednumber of characters. The char_numbers argument is optional (when using as aworksheet function), which specifies the number of characters to extract from the text string.The char_numbers value should be equal to or greater than zero; if it is greater than the

Excel and VBA Solutions

Excel & VBA

VBA Tutorial

Login

HOME EXCEL VBA ONLINE TUTORIAL EXCEL MODELS SERVICES FEEDBACK

Page 2: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

length of the text string, the LEFT function will return the text string in full; if omitted, it willdefault to 1. While using as a VBA function, it is necessary to specify both the arguments,and if text_string contains Null, the function also returns Null. Examples: Cell A1 contains the text string "James Bond". =LEFT(A1,7)Returns "James B", which are the first 7 characters. Note that space is counted as a distinctcharacter. =LEFT(A1,15)Returns "James Bond", which are all characters in cell A1, because the number 15 specifiedin the function exceeds the string length of 10 characters. =LEFT(A1)Returns "J", a single character. Not specifying the number of characters, will default to 1. -------------------------------------------------------------------------------------------------------------------------------------------------------------- RIGHT Function (Worksheet / VBA) The Excel RIGHT function can be used both as a worksheet function and a VBA function.The RIGHT function returns the specified number of characters in a text string, starting fromthe last or right-most character. Use this function to extract a sub-string from the right partof a text string. Syntax: RIGHT(text_string, char_numbers). It is necessary to mention thetext_string argument which is the text string from which you want to extract the specifiednumber of characters. The char_numbers argument is optional (when using as a worksheetfunction), which specifies the number of characters to extract from the text string. Thechar_numbers value should be equal to or greater than zero; if it is greater than the lengthof the text string, the RIGHT function will return the text string in full; if omitted, it will defaultto 1. While using as a VBA function, it is necessary to specify both the arguments, and iftext_string contains Null, the function also returns Null. Examples: Cell A1 contains the text string "James Bond". =RIGHT(A1,7)Returns "es Bond", which are the last 7 characters. Note that space is counted as a distinctcharacter. =RIGHT(A1,15)Returns "James Bond", which are all characters in cell A1, because the number 15 specifiedin the function exceeds the string length of 10 characters. =RIGHT(A1)Returns "d", a single character. Not specifying the number of characters, will default to 1. -------------------------------------------------------------------------------------------------------------------------------------------------------------- MID Function (Worksheet / VBA) The Excel MID function can be used both as a worksheet function and a VBA function. TheMID function returns the specified number of characters in a text string, starting from a

Page 3: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

specified position (ie. starting from a specified character number). Use this function toextract a sub-string from any part of a text string. Syntax: MID(text_string, start_number,char_numbers). The text_string argument is the text string from which you want to extractthe specified number of characters. The start_number argument specifies the characternumber from which to start extracting the sub-string, the first character in a text string beingstart_number 1 and incrementing towards the right. The char_numbers argument specifiesthe number of characters to extract from the text string. If start_number is greater than the length of the text string, an empty string (zero length) isreturned; if it is less than the length of the text string but together with char_numbers (ie.start_number PLUS char_numbers) it is greater than the length of the text string, the MIDfunction will return the text string in full from the start_number position to the end of the textstring. Using MID function as a worksheet function, if a negative value is specified forchar_numbers, MID will return the #VALUE! error value; if start_number is less than 1, MIDwill return the #VALUE! error value. All arguments are necessary to be specified when usingas a worksheet function. Using MID function as a VBA function: The char_numbers argument is optional whenused as VBA function, and if omitted the function will return the text string in full from thestart_number position to the end of the text string. All other arguments are necessary to bespecified when using as a vba function. If text_string contains Null, the function also returnsNull. Examples: Cell A1 contains the text string "James Bond". =MID(A1,2,6)Returns "ames B". Starts from the second character ie. "a", and then specifies that 6characters be returned starting from "a". =MID(A1,2,15)Returns "ames Bond". Returns all characters starting from the second character of "a",because the specified characters number 15 plus start number 2 (ie. total of 17) exceed thestring length of 10 characters. =MID(A1,12,2)Returns empty text (), because the start number of 12 exceeds the string length of 10characters. =MID(A1,0,7)Returns the #VALUE! error value, because the start number (ie. zero) is less than 1. -------------------------------------------------------------------------------------------------------------------------------------------------------------- LEN Function (Worksheet / VBA) The Excel LEN function can be used both as a worksheet function and a VBA function. Theworksheet LEN function returns the number of characters in a text string. Use this functionto get the length of a text string. Syntax: LEN(text_string). It is necessary to mention thetext_string argument which is the text string whose length you want to get in number ofcharacters. Note that spaces also count as characters. The worksheet LENB functionreturns the number of bytes used to represent the characters in a text string - counts eachcharacter as 1 byte except when a DBCS language [viz. Japanese, Chinese (Simplified),Chinese (Traditional), and Korean] is set as the default language wherein a character iscounted as 2 bytes. Syntax: LENB(text_string).

Page 4: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

While using LEN as a VBA function - Syntax: Len(text_string) or Len(variable_name) -you can use either a text string or a variable name, and the function will return a Long valuerepresenting the number of characters contained in the string or the number of bytesrequired to store a variable. Using the vba Len function for a variable of type variant willtreat the variable as a String and return the number of characters contained in it. Atext_string or variable containing Null, will also return Null. The vba Len function returns thenumber of characters in the string where the variable is of subtype String or Variant, andwherein the variable is of subtype numeric the function returns the number of bytes used tostore the variable. Example: Cell A1 contains the text string "James Bond". =LEN(A1)Returns 10, the string length measured by its number of characters. -------------------------------------------------------------------------------------------------------------------------------------------------------------- FIND Function (Worksheet) The FIND function returns the starting position of a text string, which it locates from within asecond text string. The starting position is the number, which is calculated beginning fromthe first character of that second text string. FIND is case-sensitive. Syntax:FIND(text_to_find, within_text, start_number). It is necessary to mention the first 2arguments, while start_number argument is optional. The text_to_find argument is the textstring that you want to find or locate, and it cannot contain any wildcard characters. Thewithin_text argument is the second text string within which you want to find or search. Thestart_number argument specifies the character number of within_text from where you wantto start the search. The first character number of within_text is 1, incrementing towards theright. Omitting the start_number argument will start your search from the first character ie.number 1. The Excel FIND function is a worksheet function. FIND is not a VBA function butcan be called or used in VBA code (worksheet functions can be called from VBA using theApplication or Application.Worksheet objects). If text_to_find cannot be located in within_text, or if start_number is specified as less than1 or greater than the length of within_text, the FIND function will return the #VALUE! errorvalue. If you specify text_to_find as empty text (""), the FIND function will match the characternumbered start_number (or 1, if start_number is omitted) in the within_text. Difference with SEARCH is that FIND is case sensitive and does not allow wildcardcharacters. If you don't want to do a case sensitive search or use wildcard characters, youcan use SEARCH. Examples: Cell A2 contains the text string "Australia". =FIND("a",A2)Returns 6, which is the position of the first small cap character "a" in the string. =FIND("A",A2)Returns 1, which is the position of the first large cap character "a" in the string.

Page 5: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

=FIND("A",A2,7)Returns the #VALUE! error value, because text "A" is not found in the string. =FIND("a",A2,7)Returns 9, which is the position of the first small cap character "a" in the string, starting fromcharacter number 7. -------------------------------------------------------------------------------------------------------------------------------------------------------------- SEARCH Function (Worksheet) The SEARCH function returns the starting position of a text string, which it locates fromwithin a second text string. The starting position is the number, which is calculated beginningfrom the first character of that second text string. SEARCH is not case-sensitive. Syntax:SEARCH(text_to_find, within_text, start_number). It is necessary to mention the first 2arguments, while start_number argument is optional. The text_to_find argument is the textstring that you want to find or locate, and it cannot contain any wildcard characters. Thewithin_text argument is the second text string within which you want to find or search. Thestart_number argument specifies the character number of within_text from where you wantto start the search. The first character number of within_text is 1, incrementing towards theright. Omitting the start_number argument will start your search from the first character ie.number 1. The Excel SEARCH function is a worksheet function. SEARCH is not a VBAfunction but can be called or used in VBA code (worksheet functions can be called fromVBA using the Application or Application.Worksheet objects). The text_to_find argument can include the wildcard characters, question mark (?) andasterisk (*). Any single character is matched using a question mark; any sequence ofcharacters are matched using an asterisk. To find an actual question mark or asterisk, type atilde (~) before the character. If text_to_find cannot be located in within_text, or if start_number is specified as less than1 or greater than the length of within_text, the FIND function will return the #VALUE! errorvalue. If you specify text_to_find as empty text (""), the FIND function will match the characternumbered start_number (or 1, if start_number is omitted) in the within_text. Difference with FIND is that SEARCH is not case sensitive but allows use of wildcardcharacters. If you want to do a case sensitive search and not use wildcard characters, youcan use FIND. Examples: Cell A2 contains the text string "Australia". =SEARCH("a",A2)Returns 1, which is the position of the first character "a" or "A" in the string. Note: using theFIND Function will return 6, because FIND will look for the first small cap character "a",being case-sensitive. Cell A5 contains the text string "Daily is every day". =SEARCH("d?y",A5)Returns 16, which is the starting character number of the first string which meets the criteria.This is the position of the first string that starts with "d" and ends with "y" and has only asingle character inbetween. =SEARCH("d*y",A5)

Page 6: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

Returns 1, which is the starting character number of the first string which meets the criteria.This is the position of the first string that starts with "d" and ends with "y" and has anynumber of characters inbetween. Note: The formula "=SEARCH("d*y",A5,2)" will return 16. -------------------------------------------------------------------------------------------------------------------------------------------------------------- REPLACE Function (Worksheet) The REPLACE function replaces part of a text string with a new text string, based onspecified number of characters and starting from a specified position. Syntax:REPLACE(old_text, start_number, number_of_chars, new_text). It is necessary to specifyall arguments. The old_text argument is the text string in which you want to replace withnew text. The start_number argument is the position of the character in old_text, which youwant to replace (ie. position of the first character from which replacement should start).Position is the character number, first character being number 1 & incrementing towards theright. The number_of_chars is the number of charactres which will be replaced in theold_text (with new_text). The new_text is the text string which will replace the characters inold_text. There is also a REPLACE Function in vba, which is not being discussed here. Refer ourVBA section for details. Example: Cell A2 contains the text string "Australia". =REPLACE(A2,8,2,"Asia")Returns "AustralAsia". Replaces 2 characters, starting with the character number 8, with"Asia". -------------------------------------------------------------------------------------------------------------------------------------------------------------- SUBSTITUTE Function (Worksheet) Use the SUBSTITUTE function to replace old text with new text in a text string. SubstituteFunction vs Replace Function: The difference is that in Substitute you replace specific textwith new text while in Replace you replace any text based on its position and length within atext string. Syntax: SUBSTITUTE(text, old_text, new_text, instance_num). It is necessaryto specify all arguments except instance_num. The text argument is the text string whichcontains the old text which you want to replace. The old_text argument is the text stringwhich you want to replace with new text. The new_text argument is the new text whichreplaces the old text. The instance_num argument is a numerical value which specifies theoccurrence of old text which you want to replace with new text. All occurrences of old textwithin the text string will be replaced with new text if you omit to specify this argument, elseonly the specified number of occurrences of the old text will be replaced with new text. Examples: Cell A2 contains the text string "Australia". =SUBSTITUTE(A2,"ia","Asia")Returns "AustralAsia". Substitutes "ia" with "Asia". Cell A4 contains the text string "Tue, Feb 01, 2011". =SUBSTITUTE(A4,1,2,3)

Page 7: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

Returns "Tue, Feb 01, 2012". Substitutes the third instance of "1" with "2". =SUBSTITUTE(A4,1,2)Returns "Tue, Feb 02, 2022". Substitutes all instances of "1" with "2". -------------------------------------------------------------------------------------------------------------------------------------------------------------- Nesting FIND within MID (Example) Cell A3 contains the text string "James Bond Daniel Craig", and we want to replace apart of the text so as to have the new string "James Bond 007". =MID(A3,1,FIND("Daniel Craig",A3)-1)&"007" [Formula]Returns the string "James Bond 007". =FIND("Daniel Craig",A3) -> This part of the formula returns 12, which is the position of"Daniel Craig" in the text. Deduct 1 from this number, to arrive at the position of the lastcharacter in the text before "Daniel Craig". =MID(A3,1,FIND("Daniel Craig",A3)-1) -> This part returns the text "James Bond ", havingdeleted the text "Daniel Craig" which we wish to replace. MID Function returns 11 characters(note that there is a space after James Bond) from the text string, starting from characternumber 1. =MID(A3,1,FIND("Daniel Craig",A3)-1)&"007" -> This completes the formula by adding thetext "007". We have now replaced the text "Daniel Craig" with "007", to return the string"James Bond 007". Nesting FIND within REPLACE (Example) Cell A3 contains the string "James Bond Daniel Craig", and we want to replace a partof the text so as to have the new string "James Bond 007". =REPLACE(A3,FIND("Daniel Craig",A3),13,"007") [Formula]Returns the string "James Bond 007". =FIND("Daniel Craig",A3) -> This part of the formula returns 12, which is the position of"Daniel Craig" in the text. =REPLACE(A3,FIND("Daniel Craig",A3),12,"007") -> The REPLACE Function replaces inthe A3 string, starting with character number 12, and replaces 12 characters ("Daniel Craig")in the old text. This completes the formula having now replaced the text "Daniel Craig" with"007", to return the string "James Bond 007". Splitting a String into Separate Cells (Examples) Example 1 We have used the LEFT, RIGHT, MID, LEN and FIND Functions to manipulate text, andsplit a string into separate cells - in the string (column A) each word is separated with acomma and a blank space, and the length of Month, Date & Year is fixed (3, 2 & 4characters respectively) while number of characters for Day vary.

Page 8: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

Table 1

Refer Table 1 - Dates in column A have been split into separate cells, in columns C toF. We take each cell at a time, and explain how it is done. Cell C2 contains the formula "=LEFT(A2,FIND(",",A2)-1)", and returns the day of week as"Tuesday". FIND(",",A2) part of the formula returns 8, which is the position of the firstoccurrence of the character "," in the string. Reducing this by 1 (to 7), we use the LEFTfunction to extract the 7-character text (which is the day of week) appearing to the left of thecharacter ",". Cell D2 contains the formula "=MID(A2,FIND(",",A2)+2,3)", and returns the month as "Feb".FIND(",",A2) part of the formula returns 8, which is the position of the first occurrence of thecharacter "," in the string. Increasing this by 2 (to 10), we use the MID function to return the3-character text (which is the month), starting from character number 10. Cell E2 contains the formula "=MID(A2,FIND(",",A2)+6,2)", and returns the date as "01".FIND(",",A2) part of the formula returns 8, which is the position of the first occurrence of thecharacter "," in the string. Increasing this by 6 (to 14), we use the MID function to return the2-character text (which is the date), starting from character number 14. Cell F2 contains the formula "=RIGHT(A2,4)", and returns the year as "2011". RIGHTreturns the last 4 characters in the text string, which is the corresponding year. Example 2 The formulas have been made more complex, by using the full month name instead of theabbreviated form. Now the number of characters in the month name will vary, whereas thelength of abbreviated month name was fixed (viz. 3).

Table 2

Refer Table 2 - Dates in column A have been split into separate cells, in columns C toF. We take each cell at a time, and explain how it is done. Cell C2 contains the formula "=LEFT(A2,FIND(",",A2)-1)", and returns the day of week as"Tuesday". FIND(",",A2) part of the formula returns 8, which is the position of the firstoccurrence of the character "," in the string. Reducing this by 1 (to 7), we use the LEFTfunction to extract the 7-character text (which is the day of week) appearing to the left of thecharacter ",". Cell D2 contains the formula "=MID(A2,FIND(",",A2)+2,LEN(A2)-9-(FIND(",",A2)+1))", andreturns the month as "February". FIND(",",A2) part of the formula returns 8, which is the

Page 9: Excel Text and String Functions_ Left, Right, Mid, Len, Find, Search, Replace, s

Excel Text and String Functions: LEFT, RIGHT, MID, LEN, FIND, SEARCH, REPLACE, SUBSTITUTE

http://www.globaliconnect.com/...113:excel-text-and-string-functions-left-right-mid-len-find-search-replace-substitute&catid=78&Itemid=474[7/10/2015 4:55:19 PM]

ContactAbout Us

TestimonialsWebsite Use Policy

© 2014 GlobaliConnect.com. All rights reserved.

position of the first occurrence of the character "," in the string. Increasing this by 2 (to 10),we use the MID function to return the text (which is the month), starting from characternumber 10. But for this, we need to know the length of this text (ie. month name), which isdetermined by "LEN(A2)-9-(FIND(",",A2)+1)". The string length of 26 [LEN(A2)] is reducedby: (i) 9, which is the number of characters after the month name ie. " 01, 2011" (note the 2spaces inbetween); and (ii) the formula part (FIND(",",A2)+1) which returns 9, which are thenumber of characters before start of month name ie. "Tuesday, " (note the 1 spaceinbetween). Therefore, length of the month name ("February") is determined to be equal to:26 minus 9 minus 9, which is 8. Cell E2 contains the formula "=MID(A2,FIND(",",A2,FIND(",",A2)+1)-2,2)", and returns thedate as "01". We use the MID function to return the 2-character text (which is the date), butthe starting character number is to be determined. The formula partFIND(",",A2,FIND(",",A2)+1)-2 determines this character number to start from. The formulaFIND(",",A2)+1 returns 9, which is the next character number after the first occurrence of ",".The formula FIND(",",A2,FIND(",",A2)+1) returns 21, whihc is the position of the secondoccurrence of ",". Reducing this by 2 (to 19) will give us the starting character number ofdate, which we had set to find out. Therefore, the MID formula to find the date, will return the2-character text date, starting from character number 19. Cell F2 contains the formula "=RIGHT(A2,4)", and returns the year as "2011". RIGHTreturns the last 4 characters in the text string, which is the corresponding year.

↑↑↑

Friday, 10 July 2015 Template designed by LernVid.com