简体   繁体   中英

Formatting excel cells - remove all except digits

Is there any way to write some formula which will remove all the characters from a cell and filter out just digits/numbers?

For example:

po63;22kv to be 6322.

It is a simple question and in some programming languages there is a method for that, so I suspect that there exist such a thing in excel as well, but so far I didn't find a solution!

Limited to a total of 15 numerics per string, array formula** :

=NPV(-0.9,IFERROR(MID(A1,1+LEN(A1)-ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1))),1)/10,""))

Format cell as General (Excel has a habit of assuming the return from NPV will be currency).

Regards

**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

After some further thought (see my comments beneath XOR's post) I have an alternative to XOR's formula - very similar in process but uses different steps to get there:

=SUBSTITUTE(TEXT(SUM(IFERROR(LEFT(RIGHT(A1,ROW(A:A)))*10^(ROW(A:A)-1),"")),"#"),"0","")

As this is an Array Formula, it must be confirmed with CTRL + SHIFT + ENTER, instead of just ENTER or TAB.

What this does is take the individual characters of the string in A1, starting with the character furthest to the right [it uses the RIGHT formula to start from the right-most character, going for an initial length of 1, then 2, etc. to the length of the string, and then it uses the LEFT character to grab the particular iteration], and it attempts to multiply that character by 10^[decimal place]. If it creates a #VALUE error it indicates that the character is a string, not a number. Otherwise, it puts each character in the decimal place associated with with its position in the original string.

It then converts the whole thing to a string and substitutes out the "0"s with "", to correct the fact that the decimal places are not yet correct. As with XOR's formula, this fails if the final number is > 10^15, because of floating-point calculation issues with Excel.

I don't think this is necessarily a simpler way to do it, just a different way, that may help you understand how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM