Home Topics excel How to remove / split text and numbers in Excel cell

How to remove / split text and numbers in Excel cell

Apr 01, 2025 am 05:07 AM

This tutorial demonstrates several methods for separating text and numbers within Excel cells, utilizing both built-in functions and custom VBA functions. You'll learn how to extract numbers while removing text, isolate text while discarding numbers, and finally, split the data into two distinct columns.

Let's say your data has a column where text and numbers are intermingled. For consistent data, simple functions like LEFT, RIGHT, and MID might suffice. However, real-world data is often inconsistent, with numbers appearing before, after, or within text. The solutions below address this complexity.

Removing Text to Keep Numbers (Excel 365, 2021, 2019):

Microsoft Excel 2019 introduced the TEXTJOIN function, which simplifies this task. The formulas below leverage TEXTJOIN, MID, ROW, INDIRECT, SEQUENCE, and IFERROR to achieve this.

For Excel 365 and 2021:

=TEXTJOIN("",TRUE,IFERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1,""))

For Excel 365 - 2019:

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

(In Excel 2019, this requires Ctrl Shift Enter for array formula entry. In dynamic array Excel, a simple Enter suffices.)

This formula iterates through each character, multiplying by 1 to convert numbers; non-numeric characters become errors, handled by IFERROR to leave empty strings. TEXTJOIN then concatenates the remaining numbers.

How to remove / split text and numbers in Excel cell

Custom VBA Function (All Excel Versions):

For older Excel versions or a simpler approach, a custom VBA function offers a streamlined solution. Two versions are provided:

VBA Code 1 (Character-by-Character):

Function RemoveText(str As String)
  Dim sRes As String
  sRes = ""
  For i = 1 To Len(str)
    If IsNumeric(Mid(str, i, 1)) Then sRes = sRes & Mid(str, i, 1)
  Next i
  RemoveText = sRes
End Function
Copy after login

VBA Code 2 (Regular Expression):

Function RemoveText(str As String) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "[^0-9]"
    RemoveText = .Replace(str, "")
  End With
End Function
Copy after login

Use these functions with =RemoveText(A2). Code 2 (using regular expressions) generally performs better on large datasets.

Removing Numbers to Keep Text (Excel 365, 2021, 2019):

Similar formulas can extract text while removing numbers. Again, using TEXTJOIN, MID, ROW, INDIRECT, SEQUENCE, IFERROR, and ISERROR:

For Excel 365 and 2021:

=TRIM(TEXTJOIN("",TRUE,IF(ISERROR(MID(A2,SEQUENCE(LEN(A2)),1)*1),MID(A2,SEQUENCE(LEN(A2)),1),"")))

For Excel 365 - 2019:

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

The TRIM function removes leading/trailing spaces.

How to remove / split text and numbers in Excel cell

Custom VBA Function for Removing Numbers (All Excel Versions):

Similar to the previous VBA functions, here are two versions to remove numbers and keep text:

VBA Code 1 (Character-by-Character):

Function RemoveNumbers(str As String)
  Dim sRes As String
  sRes = ""
  For i = 1 To Len(str)
    If Not IsNumeric(Mid(str, i, 1)) Then sRes = sRes & Mid(str, i, 1)
  Next i
  RemoveNumbers = sRes
End Function
Copy after login

VBA Code 2 (Regular Expression):

Function RemoveNumbers(str As String) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "[0-9]"
    RemoveNumbers = .Replace(str, "")
  End With
End Function
Copy after login

Use these with =TRIM(RemoveNumbers(A2)).

Splitting Text and Numbers into Separate Columns:

A single custom VBA function can handle both text and number extraction:

VBA Code 1 (Character-by-Character):

Function SplitTextNumbers(str As String, is_remove_text As Boolean) As String
  Dim sNum, sText, sChar As String
  sNum = sText = ""
  For i = 1 To Len(str)
    sChar = Mid(str, i, 1)
    If IsNumeric(sChar) Then sNum = sNum & sChar Else sText = sText & sChar
  Next i
  If is_remove_text Then SplitTextNumbers = sNum Else SplitTextNumbers = sText
End Function
Copy after login

VBA Code 2 (Regular Expression):

Function SplitTextNumbers(str As String, is_remove_text As Boolean) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    If is_remove_text Then .Pattern = "[^0-9]" Else .Pattern = "[0-9]"
    SplitTextNumbers = .Replace(str, "")
  End With
End Function
Copy after login

Use =SplitTextNumbers(A2,TRUE) to keep numbers, and =TRIM(SplitTextNumbers(A2,FALSE)) to keep text.

Using a Specialized Tool:

Consider using a dedicated Excel add-in for character removal if you prefer a more visual and user-friendly approach. The tutorial mentions an "Ultimate Suite" add-in with a "Remove Characters" feature.

This comprehensive guide provides multiple solutions catering to different Excel versions and user preferences. Remember to adjust cell references as needed for your specific data.

The above is the detailed content of How to remove / split text and numbers in Excel cell. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create timeline in Excel to filter pivot tables and charts How to create timeline in Excel to filter pivot tables and charts Mar 22, 2025 am 11:20 AM

This article will guide you through the process of creating a timeline for Excel pivot tables and charts and demonstrate how you can use it to interact with your data in a dynamic and engaging way. You've got your data organized in a pivo

how to sum a column in excel how to sum a column in excel Mar 14, 2025 pm 02:42 PM

The article discusses methods to sum columns in Excel using the SUM function, AutoSum feature, and how to sum specific cells.

how to make pie chart in excel how to make pie chart in excel Mar 14, 2025 pm 03:32 PM

The article details steps to create and customize pie charts in Excel, focusing on data preparation, chart insertion, and personalization options for enhanced visual analysis.

how to make a table in excel how to make a table in excel Mar 14, 2025 pm 02:53 PM

Article discusses creating, formatting, and customizing tables in Excel, and using functions like SUM, AVERAGE, and PivotTables for data analysis.

how to calculate mean in excel how to calculate mean in excel Mar 14, 2025 pm 03:33 PM

Article discusses calculating mean in Excel using AVERAGE function. Main issue is how to efficiently use this function for different data sets.(158 characters)

Excel formula to find top 3, 5, 10 values in column or row Excel formula to find top 3, 5, 10 values in column or row Apr 01, 2025 am 05:09 AM

This tutorial demonstrates how to efficiently locate the top N values within a dataset and retrieve associated data using Excel formulas. Whether you need the highest, lowest, or those meeting specific criteria, this guide provides solutions. Findi

how to add drop down in excel how to add drop down in excel Mar 14, 2025 pm 02:51 PM

Article discusses creating, editing, and removing drop-down lists in Excel using data validation. Main issue: how to manage drop-down lists effectively.

All you need to know to sort any data in Google Sheets All you need to know to sort any data in Google Sheets Mar 22, 2025 am 10:47 AM

Mastering Google Sheets Sorting: A Comprehensive Guide Sorting data in Google Sheets needn't be complex. This guide covers various techniques, from sorting entire sheets to specific ranges, by color, date, and multiple columns. Whether you're a novi

See all articles