Table Of Contents
How to Extract Number from String in Excel?
Splitting single-cell values into multiple cells, and collating multiple cell values into one, is the part of data manipulation. With the help of the text function in excel "LEFT," "MID," and "RIGHT," we can extract part of the selected text value or string value. To make the formula dynamic, we can use other supporting functions like "FIND" and "LEN." However, the extraction of only numbers with the combination of alpha-numeric values requires an advanced level of formula knowledge.
This article will show you the three ways to extract numbers from a string in Excel.
- #1 - Extract Number from the String at the End of the String
- #2 - Extract Numbers from Right Side but Without Special Characters
- #3 - Extract Numbers from any Position of the String
Below we have explained the different ways of extracting the numbers from strings in Excel. Read the whole article to learn this technique.
#1 - Extract the Number from the String at the End in Excel?
When we get the data, it follows a certain pattern, and having all the numbers at the end of the string is one of the patterns.
For example, the city with its zip code below is a sample of the same.
We have the city name and zip code in the above example. In this case, we know we have to extract the zip code from the right-hand side of the string. But, we do not know exactly how many digits we need from the right-hand side of the string.
Before the numerical value starts, one of the common things is the underscore (_) character. So first, we need to identify the position of the underscore character. We can do it by using the FIND method. So, apply the FIND function in excel.What text do we need to find in the find_text argument? In this example, we need to find the position of the underscore, so enter the underscore in double quotes.
The within_text is in which text we need to find the mentioned text, so select cell reference.
The last argument is not required, so leave it as of now.
So, we have got positions of underscore character for each cell. Next, we need to identify how many characters we have in the entire text. So, we must apply the LEN function in Excel to get the total length of the text value.
Now, we have total characters and positions of underscore before the numerical value. Therefore, to supply the number of characters needed for the RIGHT function, we must minus the Total Characters with Underscore Position.
Now, apply the RIGHT function in cell E2.
So, like this, we can get the numbers from the right-hand side when we have a common letter before the number starts in the string value. Instead of having so many helper columns, we can apply the formula in a single cell.
=RIGHT(A2,LEN(A2)-FIND(โ_โ,A2))
It will eliminate all the supporting columns and reduce the time drastically.
#2 - Extract Numbers From Right Side but Without Special Characters
Assume we have the same data, but this time we do not have any special character before the numerical value.
We have found a special character position in the previous example, but we do not have that luxury here. So, the formula below will find the "Numerical Position."
Please do not turn off your computer by looking at the formula. We will decode this for you.
For the SEARCH function in excel, we have supplied all the possible starting numbers of numbers, so the formula looks for the position of the numerical value. Since we have provided all the possible numbers to the array, the resulting arrays should contain the same numbers. Then, the MIN function in excel returns the smallest number among the two, so the formula reads below.
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A2&โ0123456789โณ))
So, now we have the "Numerical Position." But, first, let us find the total number of characters in the cell.
Consequently, it will return the total number of characters in the supplied cell value. Now, the "LEN" โ position of the numerical value will return the number of characters required from the right side, so apply the formula to get the number of characters.
Now, we must apply the RIGHT function in excel to get only the numerical part from the string.
Let us combine the formula in a single cell to avoid multiple helper columns.
{=RIGHT(A2,LEN(A2)-MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A2&โ0123456789โณ))+1)}
#3 - Extract Number From Any Position in Excel
We have seen from the RIGHT side extraction. But this is not the case with all the scenarios, so now we will see how to extract numbers from any string position in Excel.
For this, we need to employ various functions of Excel. Below is the formula to extract the numbers from any string position.
=IF(SUM(LEN(A2)-LEN(SUBSTITUTE(A2, {โ0โณ,โ1โณ,โ2โณ,โ3โณ,โ4โณ,โ5โณ,โ6โณ,โ7โณ,โ8โณ,โ9โ}, โโ)))>0, SUMPRODUCT(MID(0&A2, LARGE(INDEX(ISNUMBER(โMID(A2,ROW(INDIRECT(โ$1:$โ&LEN(A2))),1))* ROW(INDIRECT(โ$1:$โ&LEN(A2))),0), ROW(INDIRECT(โ$1:$โ&LEN(A2))))+1,1) * 10^ROW(INDIRECT(โ$1:$โ&LEN(A2)))/10),โโ)