Extract Numbers from a String in Excel (Using Formulas or VBA) (2024)

Watch Video – How to Extract Numbers from text String in Excel (Using Formula and VBA)

There is no inbuilt function in Excel to extract the numbers from a string in a cell (or vice versa – remove the numeric part and extract the text part from an alphanumeric string).

However, this can be done using a co*cktail of Excel functions or some simple VBA code.

Let me first show you what I am talking about.

Suppose you have a data set as shown below and you want to extract the numbers from the string (as shown below):

Extract Numbers from a String in Excel (Using Formulas or VBA) (1)

The method you choose will also depend on the version of Excel you’re using:

  • For versions prior to Excel 2016, you need to use slightly longer formulas
  • For Excel 2016, you can use the newly introduced TEXTJOIN function
  • VBA method can be used in all the versions of Excel

This Tutorial Covers:

Click here to download the example file

Extract Numbers from String in Excel (Formula for Excel 2016)

This formula will work only in Excel 2016 as it uses the newly introduced TEXTJOIN function.

Also, this formula can extract the numbers that are at the beginning, end or middle of the text string.

Note that the TEXTJOIN formula covered in this section would give you all the numeric characters together. For example, if the text is “The price of 10 tickets is USD 200”, it will give you 10200 as the result.

Suppose you have the dataset as shown below and you want to extract the numbers from the strings in each cell:

Below is the formula that will give you numeric part from a string in Excel.

=TEXTJOIN("",TRUE,IFERROR((MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1),""))

Extract Numbers from a String in Excel (Using Formulas or VBA) (2)

This is an array formula, so you need to use ‘Control + Shift + Enter‘ instead of using Enter.

In case there are no numbers in the text string, this formula would return a blank (empty string).

How does this formula work?

Let me break this formula and try and explain how it works:

  • ROW(INDIRECT(“1:”&LEN(A2))) – this part of the formula would give a series of numbers starting from one. The LEN function in the formula returns the total number of characters in the string. In the case of “The cost is USD 100”, it will return 19. The formulas would thus become ROW(INDIRECT(“1:19”). The ROW function will then return a series of numbers –{1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19}
  • (MID(A2,ROW(INDIRECT(“1:”&LEN(A2))),1)*1) – This part of the formula would return an array of #VALUE! errors or numbers based on the string. All the text characters in the string become #VALUE! errors and all numerical values stay as-is. This happens as we have multiplied the MID function with 1.
  • IFERROR((MID(A2,ROW(INDIRECT(“1:”&LEN(A2))),1)*1),””) – When IFERROR function is used, it would remove all the #VALUE! errors and only the numbers would remain. The output of this part would look like this –{“”;””;””;””;””;””;””;””;””;””;””;””;””;””;””;””;1;0;0}
  • =TEXTJOIN(“”,TRUE,IFERROR((MID(A2,ROW(INDIRECT(“1:”&LEN(A2))),1)*1),””)) – The TEXTJOIN function now simply combines the string characters that remains (which are the numbers only) and ignores the empty string.

Pro Tip: If you want to check the output of a part of the formula, select the cell, press F2 to get into the edit mode, select the part of the formula for which you want the output and press F9. You will instantly see the result. And then remember to either press Control + Z or hit the Escape key. DO NOT hit the enter key.

Download the Example File
Extract Numbers from a String in Excel (Using Formulas or VBA) (3)

You can also use the same logic to extract the text part from an alphanumeric string. Below is the formula that would get the text part from the string:

=TEXTJOIN("",TRUE,IF(ISERROR(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1),MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),""))

A minor change in this formula is thatIF functionis used to check if thearraywe get from MID function are errors or not. If it’s an error, it keeps the value else itreplaces it with a blank.

Then TEXTJOIN is used to combine all the text characters.

Caution: While this formula works great, it uses a volatile function (the INDIRECT function). This means that in case you use this with a huge dataset, it may take some time to give you the results. It’s best to create a backup before you use this formula in Excel.

Also read: How To Convert Text To Number In Excel

Extract Numbers from String in Excel (for Excel 2013/2010/2007)

If you have Excel 2013. 2010. or 2007, you can not use the TEXTJOIN formula, so you will have to use a complicated formula to get this done.

Suppose you have a dataset as shown below and you want to extract all the numbers in the string in each cell.

Extract Numbers from a String in Excel (Using Formulas or VBA) (4)

The below formula will get this done:

=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),"")

In case there is no number in the text string, this formula would return blank (empty string).

Although this is an array formula, you don’t need to use ‘Control-Shift-Enter’ to use this. A simple enter works for this formula.

Credit to this formula goes to the amazing Mr. Excel forum.

Again, this formula will extract all the numbers in the string no matter the position. For example, if the text is “The price of 10 tickets is USD 200”, it will give you 10200 as the result.

Caution: While this formula works great, it uses a volatile function (the INDIRECT function). This means that in case you use this with a huge dataset, it may take some time to give you the results. It’s best to create a backup before you use this formula in Excel.

Separate Text and Numbers in Excel Using VBA

If separating text and numbers (or extracting numbers from the text) is something you have to often, you can also use the VBA method.

All you need to do is use a simple VBA code to create a custom User Defined Function (UDF) in Excel, and then instead of using long and complicated formulas, use that VBA formula.

Let me show you how to create two formulas in VBA – one to extract numbers and one to extract text from a string.

Extract Numbers from String in Excel (using VBA)

In this part, I will show you how to create the custom function to get only the numeric part from a string.

Below is the VBA code we will use to create this custom function:

Function GetNumeric(CellRef As String)Dim StringLength As IntegerStringLength = Len(CellRef)For i = 1 To StringLengthIf IsNumeric(Mid(CellRef, i, 1)) Then Result = Result & Mid(CellRef, i, 1)Next iGetNumeric = ResultEnd Function

Here are the steps to create this function and then use it in the worksheet:

  • Go to the Developer tab.Extract Numbers from a String in Excel (Using Formulas or VBA) (5)
  • Click on Visual Basic (You can also use the keyboard shortcut ALT + F11)Extract Numbers from a String in Excel (Using Formulas or VBA) (6)
  • In the VB Editor backend that opens, right-click on any of the workbook objects.Extract Numbers from a String in Excel (Using Formulas or VBA) (7)
  • Go to Insert and click on Module. This will insert the module object for the workbook.Extract Numbers from a String in Excel (Using Formulas or VBA) (8)
  • In the Module code window, copy and paste the VBA code mentioned above.Extract Numbers from a String in Excel (Using Formulas or VBA) (9)
  • Close the VB Editor.

Now, you will be able to use the GetText function in the worksheet. Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2).

This will instantly give you only the numeric part of the string.

Extract Numbers from a String in Excel (Using Formulas or VBA) (10)

Note that since the workbook now has VBA code in it, you need to save it with .xls or .xlsm extension.

Download the Example File
Extract Numbers from a String in Excel (Using Formulas or VBA) (11)

In case you have to use this formula often, you can also save this to your Personal Macro Workbook. This will allow you to use this custom formula in any of the Excel workbooks that you work with.

Extract Text from a String in Excel (using VBA)

In this part, I will show you how to create the custom function to get only the text part from a string.

Below is the VBA code we will use to create this custom function:

Function GetText(CellRef As String)Dim StringLength As IntegerStringLength = Len(CellRef)For i = 1 To StringLengthIf Not (IsNumeric(Mid(CellRef, i, 1))) Then Result = Result & Mid(CellRef, i, 1)Next iGetText = ResultEnd Function

Here are the steps to create this function and then use it in the worksheet:

  • Go to the Developer tab.Extract Numbers from a String in Excel (Using Formulas or VBA) (12)
  • Click on Visual Basic (You can also use the keyboard shortcut ALT + F11)Extract Numbers from a String in Excel (Using Formulas or VBA) (13)
  • In the VB Editor backend that opens, right-click on any of the workbook objects.Extract Numbers from a String in Excel (Using Formulas or VBA) (14)
  • Go to Insert and click on Module. This will insert the module object for the workbook.
    • If you already have a module, double-click on it (no need to insert a new one if you already have it).Extract Numbers from a String in Excel (Using Formulas or VBA) (15)
  • In the Module code window, copy and paste the VBA code mentioned above.Extract Numbers from a String in Excel (Using Formulas or VBA) (16)
  • Close the VB Editor.

Now, you will be able to use the GetNumeric function in the worksheet. Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetText(A2).

This will instantly give you only the numeric part of the string.

Extract Numbers from a String in Excel (Using Formulas or VBA) (17)

Note that since the workbook now has VBA code in it, you need to save it with .xls or .xlsm extension.

In case you have to use this formula often, you can also save this to your Personal Macro Workbook. This will allow you to use this custom formula in any of the Excel workbooks that you work with.

In case you’re using Excel 2013 or prior versions and don’t have

You May Also Like the Following Excel Tutorials:

  • CONCATENATE Excel Ranges (with and without separator).
  • A Beginner’s Guide to Using For Next Loop in Excel VBA.
  • Using Text to Columns in Excel.
  • How to Extract a Substring in Excel (Using TEXT Formulas).
  • Excel Macro Examples for VBA Beginners.
  • Separate Text and Numbers in Excel
Extract Numbers from a String in Excel (Using Formulas or VBA) (2024)

FAQs

Extract Numbers from a String in Excel (Using Formulas or VBA)? ›

One way to extract numbers from a text string is to use the VBA InStr() function to locate the position of the first numeric character in the string, and then use the Mid() function to extract the number. For example: Dim myString As String. myString = "The total is $100.50"

How do I extract a number from a text string in Excel? ›

Select all cells with the source strings. On the Extract tool's pane, select the Extract numbers radio button. Depending on whether you want the results to be formulas or values, select the Insert as formula box or leave it unselected (default).

How do I extract specific data from a string in Excel? ›

How to extract a substring in Excel
  1. Use the LEFT, RIGHT and MID functions. You can use the LEFT, RIGHT and MID functions to extract specific text from a cell. ...
  2. Use the TRIM function. Sometimes, instead of wanting to remove text from a string, you want to remove spaces. ...
  3. Use the MID and FIND functions. ...
  4. Use Flash Fill.
Feb 3, 2023

How do I extract a number from a string? ›

The number from a string in javascript can be extracted into an array of numbers by using the match method. This function takes a regular expression as an argument and extracts the number from the string. The regular expression for extracting a number is (/(\d+)/).

How to extract numbers from text in VBA? ›

Extract Numbers from String in Excel (using VBA)

Since we have done all the heavy lifting in the code itself, all you need to do is use the formula =GetNumeric(A2). This will instantly give you only the numeric part of the string. Note that since the workbook now has VBA code in it, you need to save it with . xls or .

How do I extract a number from a string in sheets? ›

One of the primary methods for extracting numbers from strings in Google Sheets involves using the REGEXEXTRACT, REGEXREPLACE, and REGEXMATCH functions. These functions allow you to specify patterns (regular expressions) to match and extract or replace text.

How do I extract numbers from text in sheets? ›

Use REGEXEXTRACT and REGEXREPLACE functions in Google Sheets to efficiently extract numbers from cells containing mixed data types. These methods allow for the extraction of integers, decimals, and even negative numbers, streamlining data analysis tasks.

What does the Isnumber function do in Excel? ›

The Excel ISNUMBER Function[1] is categorized under Information functions. The function checks if a cell in Excel contains a number or not. It will return TRUE if the value is a number and if not, a FALSE value. For example, if the given value is a text, date, or time, it will return FALSE.

How do I access a specific value in a string? ›

You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.

How to fetch the value or data from Excel for a specific cell in a row? ›

Functions
  1. LOOKUP() The LOOKUP function finds a value in a single row or column and matches it with a value in the same position in a different row or column. ...
  2. VLOOKUP() The VLOOKUP or Vertical Lookup function is used when data is listed in columns. ...
  3. INDEX() and MATCH() ...
  4. OFFSET() and MATCH()

How do you extract characters from the middle of a string in Excel? ›

The MID Function in Excel extracts characters from the middle of a specific text. It uses the following arguments: Text (required argument), Start_num (required argument), and Num_chars (required argument).

What method is used to return a number as a string? ›

toString() The toString() method of Number values returns a string representing this number value.

What is the fastest way to convert a string to a number? ›

The fastest way to convert a string to a number in JavaScript is often using the unary plus operator (+) or Number() function.

How do you check if a character in a string is numeric in VBA? ›

VBA example

This example uses the IsNumeric function to determine if a variable can be evaluated as a number. MyVar = "53" ' Assign value. MyCheck = IsNumeric(MyVar) ' Returns True.

How do you check if a string has a numeric value? ›

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. "-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5693

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.