How to find the first number in a string
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 'String Manipulation - How to find the first number in a string 'This will return the First number found in the String (includes optional FirstPosition of string) 'This will also find decimal numbers. Public Function FindFirstNum(ByVal MyString As String, Optional ByVal FirstPos As Integer = 1) Dim I As Integer, NumFoundFlag As Boolean, StartPos As Integer Dim Char As String For I = FirstPos To Len(MyString) Char = Mid$(MyString, I, 1) If IsNumeric(Char) = True And NumFoundFlag = False Then NumFoundFlag = True StartPos = I ElseIf (IsNumeric(Char) = False And Char <> ".") And NumFoundFlag = True Then GoTo ExitHere End If Next ExitHere: FindFirstNum = Mid$(MyString, StartPos, I - StartPos) End Function 'How can I call this function: 'Debug.Print FindFirstNum("asdfsdx8a.9a,4,59sdf04dfd80 ) d99 - dk98") 'will display 8 |
Related posts:
- How to find the first number with thousand separator in a string
- How to Find First Numbers of String (along with alphas & decimals)
- How to Check whether the given string has only numbers
- How to remove Leading/Trailing Spaces from the String
- How to check if a string contains only Numeric Characters
- How to Extract only numbers from a given string
- How to find a string between two substring on a given string phrase
- How to delete a string in another comma delimited string
- How to Find a String within another String with many options
- How to Remove two or more hyphens from a string