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
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.
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.