Extract numeric portion from an alphanumeric string in Excel 2010

社会主义新天地 提交于 2020-01-05 03:33:18

问题


I would like to extract all the numbers from an alphanumeric string in excel. I have an excel sheet with list of alphanumeric strings as shown below and I would like to extract all the numbers from the alphanumeric string and store it in a new cell

I already tried the below formula found online but it outputs '6' as result but it isn't right, so can anyone please help me with it?

SUM(MID(0&A2,LARGE(ISNUMBER(-- 
MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1))*ROW(INDIRECT("1:"&LEN(A2))),
ROW(INDIRECT("1:"&LEN(A2))))+1,1)*10^ROW(INDIRECT("1:"&LEN(A2)))/10)

I would expect the output of this string:

eed1e11bd1a66cb47ad8b215c882194cdf964332484d20c56aea69e6e5196f67

to be:

1111664782158821949643324842056696519667

Please note that I wish to do this only via Excel. Preferrably some functions rather than macro.


回答1:


With data in A1, in B1 enter the array formula:

=MID(SUMPRODUCT(--MID("01"&A1,SMALL((ROW($1:$300)-1)*ISNUMBER(-MID("01"&A1,ROW($1:$300),1)),ROW($1:$300))+1,1),10^(300-ROW($1:$300))),2,300)

or even this array formula (available in Excel 365):

=TEXTJOIN("",TRUE,IF(ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),""))

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.




回答2:


Going basic and old school and long winded:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A2),"a",""),"b",""),"c",""),"d",""),"e",""),"f",""),"g",""),"h",""),"i",""),"j",""),"k",""),"l",""),"m",""),"n",""),"o",""),"p",""),"q",""),"r",""),"s",""),"t",""),"u",""),"v",""),"w",""),"x",""),"y",""),"z","")

Basically it goes through and looks for each character of the alphabet and replaces it with "" It will remain as a string in order to show leading zero. if you want it as a number, leading zero will not show and you need to send the string through a math operation that will not change its value such as:

--
+0
-0
*1
/1

The LOWER function converts the characters all to lower case. It saves doing a substitute twice. Once for lower case and once for upper case.

CAVEAT: IF there are special characters such as "!@#$%^&*()_+-=[]{}|:";'<>?, ./" the current formula will leave them untouched. A SUBSTITUTE for each special character would need to be removed. Same goes for characters like "éìô" etc




回答3:


It looks like you've only got letters a to f to remove. If that's the case, this might be quicker than a solution that handles all possible letters:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"a",""),"b",""),"c",""),"d",""),"e",""),"f","")




回答4:


Here is another formula option:

{=TEXT(NPV(-0.9,,IF(ISERR(MID(A1,256-COLUMN(A:IV),1)%),"",MID(A1,256-COLUMN(A:IV),1)%)),"0")}

Notice it's an array formula and should be confirmed through CtrlShiftEnter

Note: Scientific notation will start to become a problem, which is why I used TEXT(), but still Excel's annoying behavior on numbers will start to mess with the output from the 15th character onwards and has rounding issues or outputs as 0.

I'm thinking the smartest thing to do to prevent this is to use a UDF? Or go the old school way (@ForwardEd his answer)

Some simple UDF's could look like:

Public Function GetDigits(RNG As String) As String

GetDigits = RNG
For X = 97 To 122
    GetDigits = Replace(GetDigits, Chr(X), "")
Next X

End Function

Or use regular expressions like so:

Public Function GetDigits(RNG As String) As String

With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\D"
    GetDigits= .Replace(RNG, "")
End With

End Function



回答5:


Here is a method using an Array Formula:

=CONCAT(IFERROR(MID(A2,ROW(OFFSET($A$1,0,0,LEN(A2),1)),1)/1,""))

Literally splits the text into an array of single-characters, then checks if each is a number (i.e. can it be divided by 1).

If not, the character is with an empty string (""), and it then squishes everything back together again at the end. Remember to use Ctrl+Shift+Enter when you put it in.

(CONCAT, like TEXTJOIN is not available in Excel 2010 or earlier.)




回答6:


Here's a UDF that will handle this using REGEX (normally the fastest way to handle complex string manipulations). It removes everything that isn't a number and returns it as a string.

Function NumbersOnly(rng As Range)
 Dim nReturn As Variant
 With CreateObject("VBScript.RegExp")
        .Pattern = "[^0-9]"
        .MultiLine = True
        .Global = True
        nReturn = .Replace(rng.Value2, vbNullString)
    End With
    NumbersOnly = nReturn
End Function

If you want a number simply wrap the function in a "VALUE" function.

=VALUE(NumbersOnly(A1))


来源:https://stackoverflow.com/questions/55700335/extract-numeric-portion-from-an-alphanumeric-string-in-excel-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!