Intro
Extract numbers from text in Excel using the Remove Text Keep Numbers formula, leveraging regex, text functions, and numeric extraction techniques.
When working with data in Excel, it's common to encounter situations where you need to remove text from cells that contain a mix of numbers and text. This can be necessary for various reasons, such as cleaning up data for analysis or preparing it for use in formulas that only accept numerical values. Excel provides several ways to achieve this, including using formulas, the "Text to Columns" feature, and VBA scripts. Here, we'll focus on using Excel formulas to remove text from cells.
Understanding the Problem
Before diving into the solutions, let's understand the problem. Suppose you have a column of data where each cell contains a combination of numbers and text, and you want to extract just the numbers. For example, a cell might contain "123abc" or "xyz456", and you want to end up with just "123" or "456", respectively.
Using Formulas to Remove Text
Excel offers several formulas that can be used to remove text from cells. Here are a few approaches:
1. Using the VALUE
and SUBSTITUTE
Functions
If the text in your cells follows a predictable pattern, you might be able to use a combination of the VALUE
and SUBSTITUTE
functions to remove the text. However, this method can be cumbersome if the pattern is complex or variable.
2. Using the TEXTJOIN
and FILTERXML
Functions (Excel 2019 and Later)
For more complex scenarios, especially in newer versions of Excel, you can leverage the FILTERXML
function in combination with TEXTJOIN
to extract numbers from text.
=TEXTJOIN("",TRUE,FILTERXML(""&SUBSTITUTE(A1," "," ")&" ","//d[.*0=0]"))
This formula works by first replacing each space in the text with a closing and opening XML tag (</d><d>
), effectively wrapping each character in an XML tag. It then uses FILTERXML
to parse this XML string and select only the nodes (//d
) that contain a digit ([*0=0]
). However, this formula is primarily designed to filter out non-numeric characters based on their position or other characteristics, not directly to remove all text.
3. Using the REGEX
Function (Excel 2019 and Later with Office 365)
Excel's REGEX
function, available as part of the FILTERXML
function or directly in some versions, allows for regular expression pattern matching. You can use it to extract numbers from a string.
=TEXTJOIN("",TRUE,FILTERXML(""&SUBSTITUTE(A1,""," ")&" ","//d[matches(., '\d+')]"))
However, this formula is more about filtering than directly extracting numbers and might require adjustments based on the specifics of your data.
4. Using VBA User-Defined Function (UDF)
For more flexibility and power, especially when dealing with complex patterns or large datasets, creating a VBA User-Defined Function can be the most effective approach. Here’s a simple example of a UDF that removes all non-numeric characters from a string:
Function ExtractNumbers(s As String) As String
Dim i As Long
For i = 1 To Len(s)
If IsNumeric(Mid(s, i, 1)) Then
ExtractNumbers = ExtractNumbers & Mid(s, i, 1)
End If
Next i
End Function
To use this UDF, follow these steps:
- Open the Visual Basic Editor (VBE) by pressing
Alt + F11
or navigating to Developer > Visual Basic in the ribbon. - In the VBE, insert a new module by right-clicking on any of the objects for your workbook in the "Project" window and choosing
Insert
>Module
. - Copy and paste the UDF into the module window.
- Save the workbook as a macro-enabled file (
.xlsm
). - You can now use
=ExtractNumbers(A1)
in any cell to extract numbers from the text in cellA1
.
Practical Applications and Examples
- Data Cleaning: When importing data from external sources, it's common to encounter fields that contain unnecessary text. Using the methods described above, you can quickly clean this data to prepare it for analysis.
- Automating Reports: If you generate reports that include data extracted from mixed text and number fields, using formulas or UDFs to remove text can automate the process, saving time and reducing the chance of human error.
Advanced Techniques

Advanced Techniques

For more complex data manipulation, such as dealing with specific patterns or conditional removal of text, you might need to delve into more advanced Excel functions or even VBA scripting. The key is understanding the pattern of your data and choosing the right tool for the job.
Common Challenges and Solutions

One common challenge is dealing with data where the numbers and text are interspersed in a way that makes it hard to predict the pattern. In such cases, using regular expressions or more complex VBA scripts can provide a solution.
Best Practices for Data Management

- Plan Ahead: Consider how your data will be used in the future and try to structure it in a way that minimizes the need for extensive cleaning.
- Use Consistent Formatting: If you're generating data, try to use consistent formatting to make it easier to clean and analyze later.
Conclusion and Next Steps

Removing text from cells in Excel to keep only numbers is a common task that can be accomplished through various methods, ranging from simple formulas to more complex VBA scripts. The choice of method depends on the complexity of your data and your comfort level with Excel's functions and VBA.
Gallery of Excel Formulas for Text Removal
Excel Formulas for Text Removal Image Gallery










How do I remove text from a cell in Excel to keep only numbers?
+
You can use Excel formulas such as `FILTERXML` in combination with `TEXTJOIN`, or create a VBA User-Defined Function to remove text and keep only numbers.
What is the best method for removing text from cells in Excel?
+
The best method depends on the complexity of your data and your familiarity with Excel functions and VBA. For simple cases, formulas might suffice, while complex patterns may require VBA scripts.
How do I use the `FILTERXML` function to remove text from cells?
+
You can use `FILTERXML` in combination with `TEXTJOIN` and `SUBSTITUTE` to parse the text as XML and filter out non-numeric characters. However, this method is more about filtering based on patterns and might require adjustments for direct text removal.
How do I remove text from a cell in Excel to keep only numbers?
+You can use Excel formulas such as `FILTERXML` in combination with `TEXTJOIN`, or create a VBA User-Defined Function to remove text and keep only numbers.
What is the best method for removing text from cells in Excel?
+The best method depends on the complexity of your data and your familiarity with Excel functions and VBA. For simple cases, formulas might suffice, while complex patterns may require VBA scripts.
How do I use the `FILTERXML` function to remove text from cells?
+You can use `FILTERXML` in combination with `TEXTJOIN` and `SUBSTITUTE` to parse the text as XML and filter out non-numeric characters. However, this method is more about filtering based on patterns and might require adjustments for direct text removal.
We hope this guide has been helpful in understanding how to remove text from cells in Excel to keep only numbers. Whether you're using formulas or VBA, the key is to understand your data and choose the method that best fits your needs. If you have any further questions or would like to share your own tips for managing data in Excel, please don't hesitate to comment below.