Get First Number From String Method
The Get First Number From String method returns the numeric value of the first number that it finds in a string. If it finds no number, then it returns 0 (zero). It ignores any spaces that exist in the string.
Format
Val(string)
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
string |
A string or string expression that contains a number. |
Example
The following example examines the value of the profit variable. If profit contains a negative number, then it displays 0 (zero). The Sgn statement determines if profit is positive, negative, or zero:
Sub Button_Click
Dim profit as Single
Dim expenses
Dim sales
expenses = 100000
sales = 20000
profit = Val(sales)-Val(expenses)
If Sgn(profit) = 1 then
‘Yeah! We turned a profit!
ElseIf Sgn(profit) = 0 then
‘Okay. We broke even.
Else
‘Uh, oh. We lost money.
End If
End Sub