Extracting Numbers form a text field

DwightD

New Member
Joined
Mar 17, 2011
Messages
1
I am trying to extract the numeric values from a text field. For example, if the field is "345 6th Street Apt 84", would like to get a numeric result field of '345684'. Any ideas? :confused:
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Something like this

Code:
Public Function ExtractNumbers(str As String) As String
'loop through str and return the numbers
Dim temp As String
Dim i As Long
If Len(Nz(str, "")) > 0 Then
  For i = 1 To Len(str)
    If IsNumeric(Mid$(str, i, 1)) Then
      temp = temp & Mid$(str, i, 1)
    End If
  Next i
End If
ExtractNumbers = temp
End Function

I left the function to return a string an not a long because the parameter might be a zero-length string and it won't convert to a number. I'd call it like this

temp=ExtractNumbers(""345 6th Street Apt 84"")
If len(temp)>0 then
' do stuff here
end if

hth,

Rich
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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