Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Getting Rid of Everything Except Numbers.

Getting Rid of Everything Except Numbers

by Allen Wyatt
(last updated June 21, 2018)

7

Linda has a column that contains alpha and numeric characters. She needs to retain the numeric characters and delete the alpha ones. For example, a cell may contain 10003E111 and she wants to end up with 10003111.

There are a few ways you can approach this problem. Before proceeding with any solution, however, you should make sure that you aren't trying to change something that isn't really broken. For instance, you'll want to make sure that the "E" that appears in the number isn't part of the format of the number—in other words, a designation of exponentiation. If it is, then you don't really want to remove the character because it will end up changing the nature of the underlying number.

If you determine that the characters aren't part of the number's format, then you can first try using formulas to remove the alpha characters. If the values you want to change are in column A, you could enter the following (very long) formula in column B:

=MID(A1,MATCH(TRUE,ISERROR(1*MID(A1,ROW(INDIRECT
("1:"&LEN(A1))),1)),0),-MATCH(TRUE,ISERROR(1*MID
(A1,ABS(ROW(INDIRECT("1:"&LEN(A1)))-LEN(A1)-1),1))
,0)+LEN(A1)+2-MATCH(TRUE,ISERROR(1*MID(A1,ROW
(INDIRECT("1:"&LEN(A1))),1)),0))

Make sure you enter this as an array formula by pressing Ctrl+Shift+Enter. Then enter the following into column C:

=SUBSTITUTE(A1,B1,"")

The result is that column C contains the values from column A, without the alpha characters. You could use Paste Special to copy the information from column C to another column so that you end up with actual values instead of formula results.

This approach may work great for short-term use on a single workbook, but if you need to do this sort of data processing more often then you will want to create a user-defined function to do the processing. Here's an example:

Function OnlyNums(sWord As String)
    Dim sChar As String
    Dim x As Integer
    Dim sTemp As String

    sTemp = ""
    For x = 1 To Len(sWord)
        sChar = Mid(sWord, x, 1)
        If Asc(sChar) >= 48 And _
          Asc(sChar) <= 57 Then
            sTemp = sTemp & sChar
        End If
    Next
    OnlyNums = Val(sTemp)
End Function

You use this function by calling it from within a worksheet cell:

=OnlyNums(A1)

The function returns a numeric value. If you want to create an even shorter macro to do the processing, consider the following:

Function StripChar(aText As String)
    Dim I As Integer

    StripChar = ""
    For I = 1 To Len(aText)
        aChar = Mid(aText, I, 1)
        Select Case aChar
            Case "0" To "9"
                StripChar = StripChar & aChar
        End Select
    Next
End Function

To use this function, use either of the following in your worksheet:

=STRIPCHAR(A1)
=VALUE(STRIPCHAR(A1))

The first returns a text string consisting of the digits, the second returns the numeric version of that string.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3840) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Getting Rid of Everything Except Numbers.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Setting Table Values to Three Decimal Places

If you import information into a document from another program, the values you import may not be exactly to your liking. ...

Discover More

Easily Deploying Customizations

When you create a whole set of customizations for Excel, you may want to share them with others in your office or ...

Discover More

Compiling Misspelled Words from Documents

Word keeps track of the words that may be misspelled in a document. If you are working with a lot of documents, you may ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Automatically Breaking Text

Want to convert the text in a cell so that it wraps after every word? You could edit the cell and press Alt+Enter after ...

Discover More

Deleting Duplicate Columns

Got a worksheet in which there may be entire columns that are duplicates of each other? If you want to delete those ...

Discover More

Noting When a Workbook was Changed

Do you need to know when a workbook was last changed? There are a couple of ways you can go about keeping track of the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one more than 9?

2020-03-30 22:00:03

Monty

Formula doesnt work


2020-02-28 23:48:37

Roy

Or another version:

=TEXTJOIN("",TRUE,IFERROR(VALUE(MID(A1,SEQUENCE(LEN(A1)),1)),""))

(for them's what's got the SPILL functionss). Note that using "LEN(A1)" in the SEQUENCE() function makes it equal to the "ROW(INDIRECT..." approach in that exactly how many characters need checked will be checked, no more, no less.

And:

=TEXTJOIN("",TRUE,IFERROR(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),""))

(for them's what's not got the SPILL functions)


Neither one though lets anything but the digits 0-9 through.

To get decimal points, one could try:

=TRIM( TEXTJOIN( ,FALSE, UNICHAR( IFERROR( IF( 1 * (UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)) > 44) * (UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)) < 58) * UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)) = 0, 32, 1 * (UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)) > 44) * (UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)) < 58) * (UNICODE(MID($A$1,SEQUENCE(LEN($A$1)),1)))), 32))))

or without SEQUENCE():

=TRIM( TEXTJOIN( ,FALSE, UNICHAR( IFERROR( IF( 1 * (UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)) > 44) * (UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)) < 58) * UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)) = 0, 32, 1 * (UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)) > 44) * (UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)) < 58) * (UNICODE(MID($A$1,ROW(INDIRECT("$1:$"&LEN($A$1))),1)))), 32))))

which will keep what one might readily think are digits meant to represent discrete numbers within the string together putting a space between each grouping as well as keep decimal points and negative signs. So "KMJ883.4dhe-23" would yield "883.4 -23". This way, if those values ARE really meant to be actual numbers buried in the string, not simply digits that happen to follow one another, they can be separated out with standard techniques. Also, it TRIM()'s the result so obnoxiousness that way is gone. (My need was to separate out part dimensions from strings that were often bizarrely made, and often included foreign language characters that were of no value to me for their use. Hence the desire to keep related digits together to be numbers not characters and hence the UNICODE()/UNICHAR() angle. Did not need negative signs, and I don't keep them, but changing it to be "> 44" instead of > 45" wasn't hard...

Seems like one could take a different approach vis-a-vis what characters are kept. Mine was informed by the fact they are all in a sequence numerically, in character sets. But one could easily substitute a lookup approach ( XLOOKUP(), VLOOKUP() ) and have a table with one column, or two, first one to have the characters to keep, second to make those characters to notice, then look in the second column for the actual character to use in the output. Just a little re-jigging and the list of characters could easily be added to, etc.

Recently someone wanted to produce two strings, one with all the alpha characters removed (replaced by "double quote-space-double quote" actually), and vice versa. With just spaces instead:

Numerals only, with next-to-each-other numerals grouped as numbers in the result:

=TRIM(TEXTJOIN("",TRUE,IFERROR(VALUE(MID(A1,SEQUENCE(LEN(A1)),1))," ")))

and the text instead, kept in any side-by-side groupings:

=TRIM(TEXTJOIN("",TRUE,IF(ISERROR(VALUE(MID(A1,SEQUENCE(LEN(A1)),1))),MID(A1,SEQUENCE(LEN(A1)),1)," ")))

and one could modify either to have no spaces, easily, by replacing the final part at the end (just before the last three ")" that end the formulas) with just a pair of double quotes: "" .


2019-09-16 11:24:39

Kevin Mitchell

The formula to strip non-numerics also sees decimal points as non-numeric, and splits on the decimal.


2018-09-25 15:23:14

Stephen Sherry

Apologies for the double post but I got my version fixed.

'As Variant' instead of 'As String' solved it.

Not sure how speedy it is but I'm good with regular expressions so it's very customizable.

Function StripChar(Txt As String) As Variant
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = Val(.Replace(Txt, " "))
End With
End Function


2018-09-25 15:07:48

Stephen S

Hi, The 1st fn works well. But I'm having trouble with this. It's using regexp. I just what the fn to return a value and not a string. Thanks in advance.

Function StripChar(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = .Replace(Txt, "")
End With
StripChar = Val(Txt)
End Function


2018-01-21 03:23:01

Michael (Micky) Avidan

@To whom it may concern,
To my opinion - the suggested formula can be at least 50% shorter.
=SUM(MID(0&A1,LARGE(INDEX(ISNUMBER(--MID(A1,ROW($1:$256),1))*ROW($1:$256),),ROW($1:$256))+1,1)*10^ROW($1:$256)/10)
----------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” Excel MVP – Excel (2009-2018)
ISRAEL


2018-01-20 08:49:27

TC Pun

Thank you very much. The formula works!


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.