Link to home
Start Free TrialLog in
Avatar of engarea
engarea

asked on

Remove all Non Numeric Characters from a cell

Hi i need a solution or Function which i can use to remove all non numric characters from a cell.

For Example if a cell contains $A16 i want to be left with 16.

Your help is appreciated

Engarea
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

engarea,

Try using:

=MID(A1,FIND({"1","2","3","4","5","6","7","8","9","0"},A1,1),LEN(A1)-FIND({"1","2","3","4","5","6","7","8","9","0"},A1,1)+1)

Patrick
Avatar of Dave
Engarea

This code kills non numerics and non spaces

- press Alt & F11 to go to the VBE
- Insert Module
- paste the code below

then press Alt & F11 to return to Excel
select your range of interest
Tools - Macro - Macros and doubleclick KillNonNum

Sub KillNonNum()
    Dim C As Range, RegEx, i As Long
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Global = True
    RegEx.Pattern = "[^\d\s]+"
    For Each C In Selection
        C.Value = RegEx.Replace(C.Value, "")
    Next
    Set RegEx = Nothing
End Sub


Cheers

Dave
Avatar of engarea
engarea

ASKER

No thats not working, giving me the old #VALUE return
Avatar of engarea

ASKER

there is an error with line,

C.Value = RegEx.Replace(C.Value, "")

Also can you display this as a function so i can type into a cell for example:

=killnonnum("and the chosen cell here")
Avatar of engarea

ASKER

Yeh that is working but need it as a function not a sub

Cheers engarea
Avatar of engarea

ASKER

i have increased points to accomodate changing it to a function so i can using as a cell formulae
ASKER CERTIFIED SOLUTION
Avatar of Dave
Dave
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brett,

That's a beaut! The only hitch I found in that is that it preserves embedded spaces.
Would like to remove them too?

- stochastic
Hi stochastic,

If you want to remove all spaces change

 RegEx.Pattern = "[^\d\s]+"
to
 RegEx.Pattern = "[^\d]+"

Cheers

Dave
okay, got it. thanks.
in the first case, "remove everything except digits or spaces"
in the second case, "remove everything except digits".

- sto