I have a column of data that has verbage like so:
Individual copay is 30 etc etc.
I would like to create another column that contains just the 30.
Is there a function that I could add to my Persnal.xls that does this? Thanks for your help…
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Excel 2003 – Extract number function
How would I use this function?
Function ExtractNumber(vValue)
Dim x As Integer
For x = 1 To Len(vValue)
If Val(Mid(vValue, x)) 0 Then
ExtractNumber = Val(Mid(vValue, x))
Exit Function
End If
Next
End Function
I will be putting the code in my Personal.xls. =Personal.xls!ExtractNumber(A2). Would this be the syntax?
I have a column of data that has verbage like so:
Individual copay is 30 etc etc.
I would like to create another column that contains just the 30.
Is there a function that I could add to my Persnal.xls that does this? Thanks for your help…
Or use this found at
http://www.automateexcel.com/2008/11/03/vb…er-from-string/
Function Extract_Number_from_Text(Phrase As String) As Double Dim Length_of_String As Integer Dim Current_Pos As Integer Dim Temp As String Length_of_String = Len(Phrase) Temp = "" For Current_Pos = 1 To Length_of_String If (Mid(Phrase, Current_Pos, 1) = "-") Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If If (Mid(Phrase, Current_Pos, 1) = ".") Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If If (IsNumeric(Mid(Phrase, Current_Pos, 1))) = True Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If Next Current_Pos If Len(Temp) = 0 Then Extract_Number_from_Text = 0 Else Extract_Number_from_Text = CDbl(Temp) End If End Function
Or use this found at
http://www.automateexcel.com/2008/11/03/vb…er-from-string/Code:Function Extract_Number_from_Text(Phrase As String) As Double Dim Length_of_String As Integer Dim Current_Pos As Integer Dim Temp As String Length_of_String = Len(Phrase) Temp = "" For Current_Pos = 1 To Length_of_String If (Mid(Phrase, Current_Pos, 1) = "-") Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If If (Mid(Phrase, Current_Pos, 1) = ".") Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If If (IsNumeric(Mid(Phrase, Current_Pos, 1))) = True Then Temp = Temp & Mid(Phrase, Current_Pos, 1) End If Next Current_Pos If Len(Temp) = 0 Then Extract_Number_from_Text = 0 Else Extract_Number_from_Text = CDbl(Temp) End If End Function
I used the above function and if the sentence has more than one number, it brings both and runs them together. Where in the code can you put a space between numbers if there is more than one number? Thanks for your help.l
I used the above function and if the sentence has more than one number, it brings both and runs them together. Where in the code can you put a space between numbers if there is more than one number? Thanks for your help.l
It isn’t my function, it was just free on the net.
There is no place in this function to obviously do that.
It just extracts the numbers from a string.
It doesn’t look for multiple occurrences of a numbers and separate them.
You said you wanted to extract the number into a cell.
If you extract more than one number then the return value
would be text not a number.
What do you really want to get back out of the string.
If you want a function that extracts all Numbers out as a string separated by some character,
you could use this one.
You have to supply the Cell with the Phrase in and a suitable Separator to use when a number is found.
If only one it will be ignored
I am sure there are more elegant solutions, but it works for me.
It would be entered in the form
=ExtractNumbers(A1,” / “) if for example you wanted numbers separated by space / space
==ExtractNumbers(A1,” “) if you want them separated by a space
Function ExtractNumbers(strPhrase As String, strSeperator As String) As String Dim lngChars As Long Dim strChar As String, strNext As String Dim fNumber As Boolean Dim strReturns As String Dim lngChar As Long Application.Volatile lngChar = 1 lngChars = Len(strPhrase) Do Until lngChar > lngChars strChar = Mid(strPhrase, lngChar, 1) If lngChar = lngChars Then If IsNumeric(strChar) Then strReturns = strReturns & strChar End If Else strNext = Mid(strPhrase, lngChar + 1, 1) If strNext "." And Not IsNumeric(strNext) And IsNumeric(strChar) Then strReturns = strReturns & strChar & strSeperator ElseIf strNext = "." And IsNumeric(strChar) Then strReturns = strReturns & strChar & strNext ElseIf IsNumeric(strChar) Then strReturns = strReturns & strChar End If End If lngChar = lngChar + 1 Loop If strReturns "" And Right(strReturns, Len(strSeperator)) = strSeperator Then strReturns = Left(strReturns, Len(strReturns) - Len(strSeperator)) End If If Right(strReturns, 1) = "." Then strReturns = Mid(strReturns, 1, Len(strReturns) - 1) End If ExtractNumbers = strReturns End Function
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications