Jan
31

How to use Excel RIGHT function - formula examples

In the last few articles, we've discussed different Text functions - those that are used to manipulate text strings. Today our focus is on the RIGHT function, which is designed to return a specified number of characters from the rightmost side of a string. Like other Excel Text functions, RIGHT is very simple and straightforward, nevertheless it has a few unobvious uses that might prove helpful in your work.

Excel RIGHT function syntax

The RIGHT function in Excel returns the specified number of characters from the end of a text string.

The syntax of the RIGHT function is as follows:

RIGHT(text, [num_chars])

Where:

  • Text (required) - the text string from which you want to extract characters.
  • Num_chars (optional) - the number of characters to extract, starting from the rightmost character.
    • If num_chars is omitted, 1 last character of the string is returned (default).
    • If num_chars is greater than the total number of characters in the string, all characters are returned.
    • If num_chars is a negative number, a Right formula returns the #VALUE! error.

For example, to extract the last 3 characters from the string in cell A2, use this formula:

=RIGHT(A2, 3)

The result might look something similar to this:
Using the RIGHT function in Excel

Important note! The Excel RIGHT function always returns a text string, even if the original value is a number. To force a Right formula to output a number, use it in combination with the VALUE function as demonstrated in this example.

How to use RIGHT function in Excel - formula examples

In real-life worksheets, the Excel RIGHT function is rarely used on its own. In most cases, you will be using it together with other Excel functions as part of more complex formulas.

How to get a substring that comes after a certain character

In case you want to extract a substring that follows a specific character, use either SEARCH or FIND function to determine the position of that character, subtract the position from the total string length returned by the LEN function, and pull that many characters from the rightmost side of the original string.

RIGHT(string, LEN(string) - SEARCH(character, string))

Let's say, cell A2 contains the first and last name separated by a space, and you aim to pull the last name to another cell. Just take the generic formula above and you put A2 in place of string, and " " (space) in pace of character:

=RIGHT(A2,LEN(A2)-SEARCH(" ",A2))

The formula will yield the following result:

Right formula to extract a substring after a space

In a similar manner, you can get a substring that follows any other character, e.g. a comma, semicolon, hyphen, etc. For example, to extract a substring that comes after a hyphen, use this formula:

=RIGHT(A2,LEN(A2)-SEARCH("-",A2))

The result will look similar to this:
Right formula to extract a substring after a hyphen

How to extract a substring after the last occurrence of the delimiter

When dealing with complex strings that contain several occurrences of the same delimiter, you may often need to retrieve the text to the right of the last delimiter occurrence. To make things easier to understand, have a look at the following source data and desired result:
Source data and expected result

As you can see in the screenshot above, Column A contains a list of errors. Your goal is to pull the error description that comes after the last colon in each string. An additional complication is that the original strings may contain different numbers of delimiter instances, e.g. A3 contains 3 colons while A5 just one.

The key to finding a solution is determine the position of the last delimiter in the source string (the last occurrence of a colon in this example). To do this, you will need to use a handful of different functions:

  1. Get the number of delimiters in the original string. It's an easy part:
    • Firstly, you calculate the total length of the string using the LEN function: LEN(A2)
    • Secondly, you compute the length of the string without delimiters by using the SUBSTITUTE function that replaces all occurrences of a colon with nothing: LEN(SUBSTITUTE(A2,":",""))
    • Finally, you subtract the length of the original string without delimiters from the total string length: LEN(A2)-LEN(SUBSTITUTE(A2,":",""))

    To make sure the formula works right, you can enter it in a separate cell, and the result will be 2, which is the number of colons in cell A2.

  2. Replace the last delimiter with some unique character. In order to extract the text that comes after the last delimiter in the string, we need to "mark" that final occurrence of the delimiter in some way. For this, let's replace the last occurrence of a colon with a character that does not appear anywhere in the original strings, for example with a pound sign (#).

    If you are familiar with the syntax of the Excel SUBSTITUTE function, you may remember that it has the 4th optional argument (instance_num) that allows replacing only a specific occurrence of the specified character. And since we have already calculated the number of delimiters in the string, simply supply the above function in the fourth argument of another SUBSTITUTE function:

    =SUBSTITUTE(A2,":","#",LEN(A2)-LEN(SUBSTITUTE(A2,":","")))

    If you put this formula in a separate cell, it would return this string: ERROR:432#Connection timed out

  3. Get the position of the last delimiter in the string. Depending on what character you replaced the last delimiter with, use either case-insensitive SEARCH or case-sensitive FIND to determine the position of that character in the string. We replaced the last colon with the # sign, so we use the following formula to find out its position:
    =SEARCH("#", SUBSTITUTE(A2,":","#",LEN(A2)-LEN(SUBSTITUTE(A2,":",""))))

    In this example, the formula returns 10, which is the position of # in the replaced string.

  4. Return a substring to the right of the last delimiter. Now that you know the position of the last delimiter in a string, all you have to do is subtract that number from the total string length, and get the RIGHT function to return that many characters from the end of the original string:

    =RIGHT(A2,LEN(A2)-SEARCH("$",SUBSTITUTE(A2,":","$",LEN(A2)-LEN(SUBSTITUTE(A2,":","")))))

As shown in the screenshot below, the formula works perfectly:
Extracting a substring after the last occurrence of the delimiter

If you are working with a large dataset where different cells may contain different delimiters, you may want to enclose the above formula in the IFERROR function to prevent possible errors:

=IFERROR(RIGHT(A2,LEN(A2)-SEARCH("$",SUBSTITUTE(A2,":","$",LEN(A2)-LEN(SUBSTITUTE(A2,":",""))))), A2)

In case a certain string does not contain a single occurrence of the specified delimiter, the original string will be returned, like in row 6 in the screenshot below:
An improved formula to extract a substring after the last occurrence of the delimiter

How to remove the first N characters from a string

Apart from extracting a substring from the end of a string, the Excel RIGHT function comes in handy in situations when you want to remove a certain number of characters from the beginning of the string.

In the dataset used in the previous example, you may want to remove the word "ERROR" that appears at the start of each string and leave only the error number and description. To have it done, subtract the number of characters to be removed from the total string length, and supply that number to the num_chars argument of the Excel RIGHT function:

RIGHT(string, LEN(string)-number_of_chars_to_remove)

In this example, we remove the first 6 characters (5 letters and a colon) from the text string in A2, so our formula goes as follows:

=RIGHT(A2, LEN(A2)-6)

Using the Excel RIGHT function to remove the first 6 characters from a string

Can the Excel RIGHT function return a number?

As mentioned in the beginning of this tutorial, the RIGHT function in Excel always returns a text string even if the original value is a number. But what if you work with a numeric dataset and want the output to be numeric too? An easy workaround is nesting a Right formula in the VALUE function, which is specially designed to convert a string representing a number to a number.

For example, to pull the last 5 characters (zip code) from the string in A2 and convert the extracted characters to a number, use this formula:

=VALUE(RIGHT(A2, 5))

The screenshot below shows the result - please notice the right-aligning numbers in column B, as opposed to left-aligned text strings in column A:
Use the RIGHT function in combination with VALUE to return a number.

Why doesn't the RIGHT function work with dates?

Since the Excel RIGHT function is designed to work with text strings whereas dates are represented by numbers in the internal Excel system, a Right formula is unable to retrieve an individual part of a date such as a day, month or year.  If you attempt to do this, all you will get is a few last digits of the number representing a date.

Supposing, you have the date 18-Jan-2017 in cell A1. If you try to extract the year with the formula RIGHT(A1,4), the result would be 2753, which is the last 4 digits of number 42753 that represents January 18, 2017 in the Excel system.
The Excel RIGHT function cannot be used on dates.

"So, how do I retrieve a certain part of a date?", you may ask me. By using one of the following functions:

The following screenshot shows the results:
Use the Day, MONTH or YEAR function to get individual parts of a date.

If your dates are represented by text strings, which is often the case when you export data from an external source, nothing prevents you from using the RIGHT function to pull the last few characters in the string that represent a certain part of the date:
If a date is represented by a text string, a Right formula works correctly.

Excel RIGHT function not working - reasons and solutions

If a Right formula does not work right in your worksheet, most likely it's because of one of the following reasons:

  1. There is one or more trailing spaces in the original data. To quickly remove extra spaces in cells, use either the Excel TRIM function or the Cell Cleaner add-in.
  2. The num_chars argument is less than zero. Of course, you will hardly want to put a negative number in your formula on purpose, but if the num_chars argument is calculated by another Excel function or a combination of different functions and your Right formula returns the #VALUE! error, be sure to check the nested function(s) for errors.
  3. The original value is a date. If you have followed this tutorial closely, you already know why the RIGHT function cannot work with dates. If someone skipped the previous section, you can find full details in Why the Excel RIGHT function does not work with dates.

This is how you use the RIGHT function in Excel. To have a closer look at the formulas discussed in this tutorial, you are most welcome to download a sample Excel RIGHT function workbook. I thank you for reading and hope to see you on our blog next week.

You may also be interested in:

3 Responses to "How to use Excel RIGHT function - formula examples"

  1. Bill says:

    3 Range : Time, Value A, Value B

    Triger : “>=7”

    condition : between A and B, who reach 7 first in period of time 1 to 6, then win.

    *Eq 1 : in a period of time show by range Time 1 to 6, A hit 7 first before B. then, “A WIN“

    Time | A | B | A WIN

    1 | 1 | 2 |

    2 | 5 | 2 |

    3 | 7 | 4 |

    4 | 5 | 5 |

    5 | 4 | 6 |

    6 | 3 | 7 |

    *Eq 2 : in a period of time show by range Time 1 to 6, B hit 7 first before A. then, “B WIN“

    Time | A | B | B WIN

    1 | 1 | 2 |

    2 | 5 | 7 |

    3 | 7 | 4 |

    4 | 5 | 5 |

    5 | 4 | 6 |

    6 | 3 | 2 |

    Please help me how to make this formula, its been 3 days im trying but i still cant figure it out,

    thanks in advance.

    By: Bill

  2. Suhas Shetty says:

    Hi.. Assumed your range is A2:C7, copy and paste the below formula in cell D2 and press ctl+shift+Enter (not just "Enter")

    =IF(IFERROR(MATCH(1,IF(B2:B7>=7,1,0),0),COUNT(B2:B7))=7,1,0),0),COUNT(C2:C7)),"A Wins",IF(IFERROR(MATCH(1,IF(C2:C7>=7,1,0),0),COUNT(C2:C7))=7,1,0),0),COUNT(B2:B7)),"B Wins",IF(COUNTIF(B2:C7,">=7")=0,"Nobody Wins","Tied")))

    Hope this will solve your purpose..

  3. RAM KISHOR says:

    india-great-1900
    ind-great-12
    great-ind-130
    the Q.is that i want only numbers like 1900
    12
    130
    how can solve this is excel sheet

Post a comment



Unfortunately, due to the volume of comments received we cannot guarantee that we will be able to give you a timely response. When posting a question, please be very clear and concise. We thank you for understanding!
Ultimate Suite for Excel Professionals
 
 
50+ professional tools for Excel 2016-2007 to do your daily work
Incredible product, even better tech support…AbleBits totally delivers!
Deborah Bryant
Anyone who works with Excel is sure to find their work made easier
Jackie Lee
The best spent money on software I've ever spent!
Patrick Raugh
Ablebits is a fantastic product - easy to use and so efficient.
Debra Celmer
Excel is at its best now
Annie C.
I don't know how to thank you enough for your Excel add-ins
Jennifer Morningstar
Anybody who experiences it, is bound to love it!
Kumar Nepa
AbleBits suite has really helped me when I was in a crunch!
Nelda Fink
I have enjoyed every bit of it and time am using it
Christian Onyekachi Nwosu
It's the best $100 we've ever spent!
Mike Cavanagh
I love the program, and I can't imagine using Excel without it!
Robert Madsen
One word… WOW!
Dave Brown
Love the products!
David Johnston
It is like having an expert at my shoulder helping me…
Linda Shakespeare
Your software really helps make my job easier
Jeannie C.
Thanks for a terrific product that is worth every single cent!
Dianne Young
I love your product
Brad Gibson
Awesome!!!
Sheila Blanchard