Create new function in Excel

ssingh75

Well-known Member
Joined
Jan 5, 2012
Messages
518
Good noon to All,

Can anyone help me to create a function to remove number from column A in Excel.
e.g., Create a new function called "NoNumber"


Susheel
Excel,Access - 2007
 
Thanks again!

How can i revert it to get only number.

e.g.
=Onlynumber("Abha123y9")
Ans: 1239

Susheel
Excel,Access - 2007
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try

Code:
Function OnlyNumber(r As Range) As Long
Dim i As Long, x As String
For i = 1 To Len(r.Value)
    If Asc(Mid(r.Value, i, 1)) >= 48 And Asc(Mid(r.Value, i, 1)) <= 57 Then x = x & Mid(r.Value, i, 1)
Next i
OnlyNumber = Val(x)
End Function
 
Upvote 0
Here's an alternative you may also wish to consider. Whilst the written code is not as short, it doesn't require looping through each character in each cell.

It handles both cases by the use of an optional second argument in the fuction. If the second argument is TRUE or 1 or omitted, the function returns the digits from the cell value. If the second argument is FALSE or 0 the function returns the non-digits from the cell value.

I'm not sure if it will be possible with your data or what result you would want, but this function returns a null string if the target cell contains no digits. See rows 2 & 3 in my sample below. VoG's code returns 0 for that type of cell.

<font face=Courier New><br><SPAN style="color:#00007F">Function</SPAN> Separate(<SPAN style="color:#00007F">ByVal</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, <SPAN style="color:#00007F">Optional</SPAN> Numbers <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN> = <SPAN style="color:#00007F">True</SPAN>)<br>    <SPAN style="color:#00007F">Static</SPAN> RegEx <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> t <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <br>    <SPAN style="color:#00007F">If</SPAN> RegEx <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> RegEx = CreateObject("VBScript.RegExp")<br>        RegEx.Global = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    RegEx.Pattern = IIf(Numbers, "\D", "\d")<br>    t = RegEx.Replace(s, "")<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    t = <SPAN style="color:#00007F">CLng</SPAN>(t)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    Separate = t<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>


Example of use in the sheet. All formulas copied down.

Excel Workbook
ABCDEF
1Abha123y9123912391239AbhayAbhay
2
3abc dabc dabc d
4575575575575
Separate Text & Digits
 
Upvote 0

Forum statistics

Threads
1,223,743
Messages
6,174,244
Members
452,553
Latest member
red83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top