Вы находитесь на странице: 1из 22

Text Manipulation Formulas

https://www.vertex42.com/blog/excel-formulas/text-formulas-in-excel.html

This workbook contains examples from the article "Text Manipulation Formulas in Excel." Regarding copyright
file like a book. You may use the ideas and techniques and formulas explained here, but you may not reproduc
substantial portions from it, just as you would not do so with a book. Thank you. - Jon Wittwer

EXAMPLES

LEN: Get the length of a text string (number of characters)

Formula: =LEN(text)

UPPER, LOWER, PROPER: Change the case of a text string

Formula: =UPPER(text)
Formula: =LOWER(text)
Formula: =PROPER(text)

EXACT: Case-sensitive text comparisons

Formula: =EXACT(text,UPPER(text))
Formula: =EXACT(text,LOWER(text))
Formula: =EXACT(text,PROPER(text))

CONCATENATE: Combine text to create a string


Formula: =CONCATENATE("Hi"," ","World")

Formula: ="Hi"&" "&"World"

NOTE When used for concatenation, the & character is used as an "operator" rather than a functio
operators are +, -, and /.

The CONCAT and TEXTJOIN functions are also useful for concatenating strings, but are av
365 subscription using Excel 2016 or Excel Online.

Formula: =CONCAT(A1:B1)

Formula: =TEXTJOIN(", ", TRUE, A1:B1)

CHAR, UNICHAR: Return a special character based on a numeric


When you concatenate text and need to include double quotes, you can use the CHAR(34) or UNICHAR(34) f
function lets you return the character for a given numeric code (most of the codes correspond to ASCII values
does the same thing for decimal Unicode values (UTF-16). The CODE and UNICODE functions do the reverse
decimal numeric value for a given character.

The UNICHAR and UNICODE functions are available in Excel 2013 or later.

Use CHAR(10) or UNICHAR(10) to return the line break character - and remember to toggle Word Wrap for th
the line break.

Code CHAR CODE UNICHAR UNICODE


34 " 34 " 34
182 � 239 ¶ 182
169 � 239 © 169
149 � 239 • 149
10004 Err:502 Err:502 ✔ 10004
128515 Err:502 Err:502 � 128515

Formula: =CHAR(34) & value & CHAR(34)


Formula: ="Hi" & CHAR(10) & "World"

SEE ALSO Using Unicode Characters in Excel

REPT: Repeat a text string N times


The REPT function creates a text string by repeating a character or string a number of times. The following ex
REPT("★",num) to create a dot plot.

Votes Dot Plot


5 ★★★★★
9 ★★★★★★★★★
3 ★★★
6 ★★★★★★

Create a Progress bar without Conditional Formatting

You can create a progress bar using an in-cell bar via conditional formatting, but if that method is not available
reasons), you can represent the progress as a discrete fraction like n/10 and use REPT to create an in-cell pro
idea to use "empty" character symbols to represent the incomplete portion of the progress bar because the re
exactly into a cell like a conditional formatting progress bar.

Formula: =REPT("☒",ROUND(progress*10,0)) & REPT("☐",ROUND((1-progress)*10,0))

Progress Using REPT


35.0% ☒☒☒☐☐☐☐☐☐☐
75.0% ⚑⚑⚑⚑⚑⚑⚑⚑⚐⚐

Create a Sprint-style Progress bar

When combined with concatenation and special unicode characters, you can create an in-cell "sprint chart" lik
sprint chart is just a progress bar that shows the progress relative to the amount of time left. The time can be r
a watch, clock or hourglass symbol.

Date Start Days Today Progress In-Cell Sprint Chart


12/1/2017 14 12/10/2017 28% ⚑⚑⚑⚑⚐⚐⚐⚐⚐⌚⚐⚐⚐⚐⚐
⚐⚐

Symbols Others
Complete: ⚑ ☒ , ⚑,
Incomplete: ⚐ ☐, ⚐
Today: ⌚ ⌚, ⌛, �
Finish: ⚐

Formula: sprint = 14
progress = 24.5%
complete = ROUND(sprint*progress,0)
time = IF(TODAY()<start_date,0,IF(TODAY()>(start_date+sprint),sprint,TODAY()-start_date)
=REPT("⚑",MIN(time,complete)) & REPT("⚐",MAX(0,time-complete)) & "⌛" &
REPT("⚑",MAX(0,complete-time)) & REPT("⚐",sprint-MAX(time,complete)) " & ""

Why do some unicode characters like the watch ⌚ and hourglass ⌛ and finishline flag change to other chara
formula? I have no idea.

Team Progress
A 40% ⚑⚑⚑⚑⚑⚑⚐⚐⚐⌚⚐
⚐⚐⚐⚐⚐
⚐⚐
B 73% ⚑⚑⚑⚑⚑⚑⚑⚑⚑⌚⚐
⚑⚐⚐⚐⚐
⚐⚐
C 65% ⚑⚑⚑⚑⚑⚑⚑⚑⚑⌚⚐
⚐⚐⚐⚐⚐
⚐⚐
D 56% ⚑⚑⚑⚑⚑⚑⚑⚑⚐⌚⚐
⚐⚐⚐⚐⚐
⚐⚐

REFERENCE Using Unicode Characters in Excel

TRIM, CLEAN, SUBSTITUTE: Remove extra spaces and other cha


TRIM removes all spaces (ASCII character 32) except for one space between words.

Formula: =TRIM(text)

If you also need to remove nonprinting characters such as tabs, newlines, etc. you can use the CLEAN functio
characters 0-31.
Formula: =CLEAN(text)

You can use the SUBSTITUTE function when cleaning up text if you need to remove other types of non-ASCII
replace the characters with normal spaces. Wrap the function within TRIM to remove extra spaces. This is a g
and newlines (10) and non-breaking spaces (160) so that you don't combine words that should be separated b
below nests the SUBSTITUTE function multiple times to get rid of tabs, newlines, and non-breaking spaces wi

Formula: =TRIM( SUBSTITUTE( SUBSTITUTE( SUBSTIT


UTE(text,CHAR(160)," "),CHAR(10),"
"),CHAR(9)," ") )

FIND, SEARCH: Get the position of text within a string


The FIND and SEARCH functions will return the starting character position of a text string within another string
sensitive and FIND is not. The default for the [start_num] argument is 1 (the beginning of the string).

Case Sensitive
Formula: =FIND(find_text,within_text,[start_num])
=FIND(" ","Tom Sawyer")

NOT Case Sensitive


Formula: =SEARCH(find_text,within_text,[start_num])
=SEARCH(" ","Tom Sawyer")

The [start_num] argument can come in handy if you want to find the position of the 2nd occurrence of text with
this by using a nested FIND or nested SEARCH formula, like this:

Find the Position of the 2nd Space


Formula: =FIND(" ",text,FIND(" ",text,1)+1)

Find the Position of the 2nd "A"


Formula: =SEARCH("A",text,SEARCH("A",text,1)+1)
SUBSTITUTE: Replace the Nth or each occurrence of text within a
The SUBSTITUTE function is very powerful, especially because it can be used to replace the Nth occurrence
other character. If the Nth occurrence is not specified, it will replace every occurrence.

Formula: =SUBSTITUTE(original_text,old_text,new_text,[occurrence])

old_text new_text
Replace each occurrence: # ,
old_text new_text
Replace only the 2nd occurrence: # ,

When combined with FIND or REPLACE, you can use SUBSTITUTE to find the position of the Nth occurrence
text string.

Find the Position of the 3rd space in the text string


Formula: =FIND("#",SUBSTITUTE(text," ","#",3),1)

Text: Tim A. J. Crane

MID, REPLACE: Extract or replace text based on position and leng


The MID function is like the substr() function in other coding languages. It extracts a string from within another
starting character position and the number of characters. The REPLACE function is similar, except that instead
replaces the text with a string that you specify.

Formula: =MID(text,start_num,num_chars)

start_num num_chars
5 3

Formula: =REPLACE(text,start_num,num_chars,replace_text)

start_num num_chars
3 1
LEFT, RIGHT: Get a number of characters starting from the left or r
The LEFT and RIGHT functions are like shorthand versions of the MID function. The LEFT function extracts te
the string and RIGHT function extracts text starting from the end of the string.

Formula: =LEFT(text,num_chars)
Formula: =RIGHT(text,num_chars)

Count the number of spaces in a text string


You can use this technique to count other characters besides spaces. Just substitute " " with "," or ";" to count
semi-colons for example.

Formula: =LEN(text)-LEN(SUBSTITUTE(text," ",""))

Count the number of occurrences of a string within a string


If you want to count the number of occurrences of a string within a string, then you can use a slightly modified
formula. In this case, you'll need to divide the result by the length of string.

Extract a Last Name


Formula: =(LEN(text)-LEN(SUBSTITUTE(text,string",""))) /
LEN(string)

Extract a First Name


To extract the first word or name from a text string, you can use the LEFT function and then use FIND to return
space. Subtracting 1 gives you the number of characters in the first word or name.
Extract a First Name
Formula: =LEFT(text,FIND(" ",text)-1)

It a space is not found, FIND will return an error, so you can wrap the formula with ISERROR to return the full
found, like this:

Formula: =IFERROR( LEFT(text,FIND(" ",text)-1), text)

Extract Text After the First Space


To return the text after the first space, you can use the RIGHT function and then calculate the length of the las
FIND(" ",text). If a name only contains a first and last name, this would return the last name, but if it is a name
will return "Allen Smith".

Formula: =RIGHT(text,LEN(text)-FIND(" ",text))

Using the MID function instead of RIGHT


Formula: =MID(text,FIND(" ",text)+1,9999999)

Extract the Last Name


The return the last name in a string when there may be more than two names, we replace FIND(" ",text) with t
space. To do that, first count the number of spaces using the formula mentioned above, then SUBSTITUTE th
special character and use FIND to get the position of that special character. You can use any character that yo
name. The example below uses the "!" character.

Extract a Last Name


Formula: =IFERROR( RIGHT(text,LEN(text)-
FIND("!",SUBSTITUTE(text," ","!",LEN(text)-
LEN(SUBSTITUTE(text," ",""))))), "")
=IFERROR( RIGHT(text,LEN(text)-
FIND("!",SUBSTITUTE(text," ","!",LEN(text)-
LEN(SUBSTITUTE(text," ",""))))), "")

I've wrapped the formula with IFERROR because if the name contains no spaces, I assume the name is only
empty string "". You could instead use IFERROR to return the original text value instead of "".

REFERENCE support.office.com: Split text into different columns with functions

Return the Nth word in a string


This function is really crazy, but useful. Basically what is going on is that you replace the delimiter with a bunch
you create a new text string that can be divided into chunks, where each chunk contains a different word. The
surrounding each word, so you use TRIM to remove it.

Formula: =TRIM(MID(SUBSTITUTE(text,delimeter,REPT("
",LEN(text))),(word-1)*LEN(text)+1,LEN(text)))

REFERENCE http://www.mrexcel.com/forum/excel-questions/587534-text-columns-via-formula.html

REFERENCE https://exceljet.net/formula/extract-nth-word-from-text-string

Convert a Text String to an Array of Words


Want to convert "One#Two#Three" into an array like {"One";"Two";"Three"} that can be used within array form
formula in the previous tip and replace "word" with the following:

Formula: word = ROW(INDIRECT("1:"&((LEN(text)-LEN(SUBSTITUTE(text,delimiter,"")))/LEN(delimi

The final formula will look like this:

Formula: =TRIM(MID(SUBSTITUTE(text,delimieter, REPT(" ",LEN(text))), (ROW(INDIRECT("1:"&((L


LEN(SUBSTITUTE(text,delimiter,"")))/LEN(delimiter)+1))) -1) * LEN(text)+1, LEN(text)))

To display the results within an array of cells, remember to use Ctrl+Shift+Enter. Use TRANSPOSE if you wan
this formula in a row instead of a column.
delimiter

##

REFERENCE https://stackoverflow.com/questions/25316094/split-a-string-cell-in-excel-without-vba-e-g-for

Convert a Text String to an Array of Characters


The following function will split a text string into an array of separate characters.

Formula: =MID(text_string, ROW(INDIRECT("1:"&LEN(text_string))), 1)

text Results (entered as a multi-cell array formula)


3B9Q 3 B 9 Q

If you wrap the function with CODE or UNICODE then you can display the numeric code for each character in

Formula: =UNICODE( MID(text_string, ROW(INDIRECT("1:"&LEN(text_string))), 1) )

text Results (entered as a multi-cell array formula)


!"= 33 32 34 32

In Excel 2016 with an Office 365 Subscription, you can wrap the above functions with TEXTJOIN to display a
separated by a delimiter.

Formula: =TEXTJOIN(",",TRUE, UNICODE(MID(text,ROW(INDIRECT("1:"&LEN(text)) ),1)) )

text Result Entered as an Array Formula


!"= #NAME?

Experiment with Flash Fill or Text-to-Columns


This example is provided for you to experiment with using flash fill (Ctrl+e) or the text-to-columns feature.

Formula in Image for Blog Post "Text Manipulation with Excel Form

Start With

Result

REFERENCES

Some references have been included above. See the support.office.com website for official documentation of
© 2017 Vertex42 LLC

mulas in Excel." Regarding copyright and sharing, think of this


ained here, but you may not reproduce this worksheet or copy
nk you. - Jon Wittwer

of characters)

Text Result
onetwothree 11

se of a text string

Text Result
this text THIS TEXT
THIS TEXT this text
this text This Text

Text Result
THIS TEXT 1
this text 1
This Text 1
Value 1 Value 2 Result
Hi World Hi World

Hi World Hi World

as an "operator" rather than a function. Other examples of

l for concatenating strings, but are available only with an Office

Hi World #NAME?

A B #NAME?

er based on a numeric code.


use the CHAR(34) or UNICHAR(34) function. The CHAR
he codes correspond to ASCII values). The UNICHAR function
nd UNICODE functions do the reverse and will tell you the

ter.

remember to toggle Word Wrap for the cell to see the effect of

Value Result
Hello "Hello"

With Word Wrap Without


Hi Hi
World World

g a number of times. The following example uses

ng, but if that method is not available to you (for compatibility


and use REPT to create an in-cell progress chart. It's a good
n of the progress bar because the resulting string will not fit

UND((1-progress)*10,0))

Using Conditional Formatting


35.0%
75.0%

can create an in-cell "sprint chart" like the following example. A


amount of time left. The time can be represented in the chart as

ell Sprint Chart


⚑⚑⚑⚐⚐⚐⚐⚐⌚⚐⚐⚐⚐⚐
⚐⚐

ate+sprint),sprint,TODAY()-start_date))
time-complete)) & "⌛" &
MAX(time,complete)) " & ""

finishline flag change to other characters when used in a

⚑⚑⚑⚑⚑⚐⚐⚐⌚⚐
⚐⚐⚐⚐⚐
⚐⚐
⚑⚑⚑⚑⚑⚑⚑⚑⌚⚐
⚑⚐⚐⚐⚐
⚐⚐
⚑⚑⚑⚑⚑⚑⚑⚑⌚⚐
⚐⚐⚐⚐⚐
⚐⚐
⚑⚑⚑⚑⚑⚑⚑⚐⌚⚐
⚐⚐⚐⚐⚐
⚐⚐

a spaces and other characters


ween words.

Text Result
hi world hi world

s, etc. you can use the CLEAN function which removes all ASCII
Text Result
Hi
HiWorld
World

d to remove other types of non-ASCII characters or want to


M to remove extra spaces. This is a good way to handle tabs (9)
bine words that should be separated by a space. The example
ewlines, and non-breaking spaces within one formula.

Text Result
Hi Hi World
World

in a string
on of a text string within another string. SEARCH is case
the beginning of the string).

within_text find_text Result


ooAooaoo a 6
Tom Sawyer 4

within_text find_text Result


ooAooaoo a 3
Tom Sawyer 4

tion of the 2nd occurrence of text within a string. You can do

text Result
Jim A. Swift 7

within_text find_text Result


ooAooaoo A 6
urrence of text within a string
used to replace the Nth occurrence of a character with some
y occurrence.

original_text Result
1#2#3#4 1, 2, 3, 4
original_text occurrence Result
1#2#3#4 2 1#2, 3#4

ind the position of the Nth occurrence of a string within another

Result
10

ed on position and length


t extracts a string from within another string by specifying the
function is similar, except that instead of extract the text, it

text value Result


one#two#three two

text value replace_text Result


1#2#3 BLAH 1#BLAH#3
tarting from the left or right
nction. The LEFT function extracts text starting from the left of
ring.

text num_chars Result


Hi World 4 Hi W
Hi World 4 orld

st substitute " " with "," or ";" to count the number of commas or

Name Delimeter Result


Todd Allen Smith 2

within a string
then you can use a slightly modified version of the above
g.

Name String Result


A##B##C ## 2
A, B, C , 2

T function and then use FIND to return the position of the first
or name.
Name Result
Tom Sawyer Tom
Tom #VALUE!

mula with ISERROR to return the full text value if no spaces are

Name Result
Tom Tom

nd then calculate the length of the last name using LEN(text)-


turn the last name, but if it is a name like "Todd Allen Smith" it

Name Result
Tom Sawyer Sawyer
Todd Allen Smith Allen Smith

Name Result
Tom Sawyer Sawyer
Todd Allen Smith Allen Smith

ames, we replace FIND(" ",text) with the position of the last


ntioned above, then SUBSTITUTE the last space with another
er. You can use any character that you know won't be in the

Name Result
Todd A. B. Smith Smith
Todd Smith Smith
Todd

o spaces, I assume the name is only a first name, so I return the


xt value instead of "".

you replace the delimiter with a bunch of blank spaces so that


chunk contains a different word. There will be a lot of space

Name delimiter word Result


One#Two#Three # 1 One
One#Two#Three # 2 Two
One#Two#Three # 3 Three

4-text-columns-via-formula.html

"} that can be used within array formulas? First, start with the

TITUTE(text,delimiter,"")))/LEN(delimiter)+1)))

EN(text))), (ROW(INDIRECT("1:"&((LEN(text)-
1))) -1) * LEN(text)+1, LEN(text)))

+Enter. Use TRANSPOSE if you want to display the results of


text Results (entered as an array formula)
Todd Allen Smith Todd Allen Smith
Todd Smith Todd Smith #N/A
Todd Todd Todd Todd
A##B##C A B C

string-cell-in-excel-without-vba-e-g-for-array-formula

acters.

#N/A #N/A #N/A

e numeric code for each character in the string.

EN(text_string))), 1) )

61 #N/A #N/A

unctions with TEXTJOIN to display a list as a text string

RECT("1:"&LEN(text)) ),1)) )

rray Formula
e) or the text-to-columns feature.

Name First Middle Last


Todd Allen Smith Todd Allen Smith
Tom Sawyer
Huck
A. J. Aimes
Mr. Johnson

ulation with Excel Formulas"

Text Manipulation

Excel Formulas

website for official documentation of Excel functions.

Вам также может понравиться