|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
提取数字自定义函数代码:应用时注意宏的相关设置,此函数有连个参数,第一个是提取的数据源,第二个是数据源中有从第几个数字开始提取!!试试吧!!
Option Explicit
Function GetNumber(rng As Range, Pos As Integer) As Single
Dim i As Integer
Dim x As Integer
Dim t As Integer
Dim ilen As Integer
Dim StartPos As Integer
Dim EndPos As Integer
Dim sStr As String
Dim myStr As String
Dim numArray() As String
Application.Volatile
x = 0
t = 0
myStr = "0123456789."
sStr = Trim(rng.Value)
ilen = Len(sStr)
For i = 1 To ilen
Do
x = x + 1
While InStr(1, myStr, Mid(sStr, x, 1)) < 1 And x <= ilen
x = x + 1
Wend
StartPos = x
While InStr(1, myStr, Mid(sStr, x, 1)) > 0 And x <= ilen
x = x + 1
Wend
EndPos = x - 1
If StartPos <= EndPos And IsNumeric(Mid(sStr, StartPos, EndPos - StartPos + 1)) Then
t = t + 1
ReDim Preserve numArray(1 To t)
numArray(t) = Mid(sStr, StartPos, EndPos - StartPos + 1)
End If
Loop Until x >= ilen
Next
On Error GoTo exitfun
sStr = numArray(1)
On Error GoTo 0
If Pos > UBound(numArray) Then
GetNumber = 0
Else
GetNumber = numArray(Pos)
End If
Exit Function
exitfun:
GetNumber = 0
End Function |
|