我找到了一个名为FindNum的可视化基本代码,它将为您提供包含文本和数字的字符串的编号。
我真的希望函数在字符串中没有包含任何数字时返回一个数字。 (我已经记下了我的想法)。 http://imgur.com/LuP0KNN
这是视觉基本代码。如果有人能提供帮助,我真的很感激。
Function FindNum(parameter, Optional ignore As Variant, Optional side As Variant) As Double
Dim n
Dim i
Dim p
i = 0
If IsMissing(ignore) = True Then
p = parameter
Else
If side = 1 Then
p = Right(parameter, (Len(parameter) - Len(ignore)))
ElseIf side = 0 Then
p = Left(parameter, (Len(parameter) - Len(ignore)))
Else
p = parameter
End If
End If
If IsNumeric(Left(p, (Len(p) - (Len(p) - 1)))) = True Then
Do
n = Left(p, (Len(p) - i))
i = i + 1
Loop Until IsNumeric(n) = True
FindNum = n
Else
Do
n = Right(p, (Len(p) - i))
i = i + 1
Loop Until IsNumeric(n) = True
FindNum = n
End If
End Function
答案 0 :(得分:1)
我做了类似的功能。它可以从左到右或从右到左阅读,你可以设置" ertdfgcvb"如果您愿意,可以是小数点分隔符。
Function FindNum2(num As String, Optional DecimalSeparator As String, Optional FromRight As Boolean) As Double
' text where the number is | string that works as decimal sep | who the hell wants to read from right to left? if you do, this one's for you
If Len(num) = 0 Then Exit Function
Dim x As String, y As String, DefaultValue As Double
DefaultValue = 1 'here's your default
num = Replace(num, DecimalSeparator, ".")
For i = IIf(FromRight, Len(num), 1) To IIf(FromRight, 1, Len(num)) Step IIf(FromRight, -1, 1)
y = Mid(num, i, 1) 'the current character
If y Like "#" Then 'if it's a number
x = x & y 'then append it
ElseIf y = "." Then 'if it's a decimal separator
x = x & y 'then append a decimal separator; if there are multiple of it then tough luck
End If
Next i
If x = "" Then
FindNum2 = DefaultValue
Else
FindNum2 = Val(x) 'converts the string to a double
End If
End Function
答案 1 :(得分:0)
将支票更改为for循环并检查每个char isNumeric。然后检查你的计数器变量。
Function FindNum(parameter, Optional ignore As Variant, Optional side As Variant) As Double
Dim n
Dim i
Dim p
Dim Counter As Integer
i = 0
If IsMissing(ignore) = True Then
p = parameter
Else
If side = 1 Then
p = Right(parameter, (Len(parameter) - Len(ignore)))
ElseIf side = 0 Then
p = Left(parameter, (Len(parameter) - Len(ignore)))
Else
p = parameter
End If
End If
For Counter = 1 To Len(p)
If IsNumeric(Mid(p, Counter, 1)) = True Then
n = n + Mid(p, Counter, 1)
End If
Next
If n = "" Then
FindNum = 1
Else
FindNum = n
End If
End Function
答案 2 :(得分:0)
看看这是否适合你:
Function FindNum(str As String, Optional FromLeft As Boolean = False, Optional default As Long = 1) As Long
Dim i As Long, n As Long, pieces As Variant
Dim ch As String, newstr As String
n = Len(str)
For i = 1 To n
ch = Mid(str, i, 1)
If IsNumeric(ch) Then
newstr = newstr & ch
Else
newstr = newstr & "/"
End If
Next i
pieces = Split(newstr, "/")
On Error GoTo return_default
n = UBound(pieces)
If FromLeft Then
For i = 0 To n
If Len(pieces(i)) > 0 Then
FindNum = Val(pieces(i))
Exit Function
End If
Next i
Else
For i = n To 0 Step -1
If Len(pieces(i)) > 0 Then
FindNum = Val(pieces(i))
Exit Function
End If
Next i
End If
return_default:
FindNum = default
End Function
这将从左侧或右侧返回字符串中检测到的第一个整数(取决于可选参数,如果只找到1个整数则没有区别),如果没有找到这样的数字,则返回默认值。