特定范围的单元格包含Check. 3 months
或Dep. 2 months
形式的信息。我需要嵌入vba,以便读取这些单元格,每次提取,即输出,它包含的数量。
编辑:我知道当你知道自己在寻找什么时会有功能,但是我所描述的我什么都不知道
你能帮忙吗,因为我过去没有遇到类似的任务。
答案 0 :(得分:1)
您可以遍历字符串中的每个字符并检查它是否为数字。这适用于值0-9。如果有更高的值,您需要调整此值以查找多个数字,而不是在找到第一个数字后退出。
Public Function GetMyDigit(cell As Range) As Integer
Dim s As String
Dim i As Integer
'get cell value
s = cell.Value
'loop through the entire string
For i = 1 To Len(s)
'check to see if the character is a numeric one
If IsNumeric(Mid(s, i, 1)) = True Then
'set the function to the value and exit
GetMyDigit = Mid(s, i, 1)
Exit Function
End If
Next i
End Function
如果你有多个数字
Public Function GetMyDigit(cell As Range)
Dim s As String
Dim i As Integer
Dim answer
'get cell value
s = cell.Value
'loop through the entire string
For i = 1 To Len(s)
'check to see if the character is a numeric one
If IsNumeric(Mid(s, i, 1)) = True Then
'set the function to the value and exit
answer = answer & Mid(s, i, 1)
End If
Next i
GetMyDigit = answer
End Function
答案 1 :(得分:1)
只是提供另一种方法:
Function findNumber(inPtStr As String) As Double
Dim strArr() As String
Dim i As Long
strArr = Split(inPtStr)
For i = LBound(strArr) To UBound(strArr)
If IsNumeric(strArr(i)) Then
findNumber = --strArr(i)
Exit Function
End If
Next i
End Function