读取一个单元格的值并提取它所包含的数字

特定范围的单元格包含Check. 3 monthsforms的Check. 3 months Check. 3 months或部门Dep. 2 months Dep. 2 months 。 我需要embeddedVBA所以读取这些单元格,每次提取,这是输出,它包含的数字。

编辑:我知道有function,当你知道你在寻找什么,但我所描述的我什么都没有意识到

请问您能帮忙,因为我以前没有遇到类似的任务。

您可以遍历string中的每个字符,并检查它是否是数字。 这将适用于值0-9。 如果有更高的值,则需要调整它以查找多个数字,并且一旦find第一个数字就不会退出。

 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 

只是为了给另一种方法:

 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