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.
Written by Allen Wyatt (last updated June 17, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
When processing data, you may have a need to split a long text string into shorter chunks of text consisting of whole ...
Discover MoreWhen you insert rows, columns, or cells in a worksheet, does the resulting Insert Options icon bother you? Here's how to ...
Discover MoreExcel has a great (and little known) shortcut for filling a column with information. It comes in very handy when you need ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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!
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments