简体   繁体   English

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

[英]Read a cell's value and extract the number that it is contained

The cells of a specific range contain information of the form Check. 3 months 特定范围的单元格包含Check. 3 months形式的Check. 3 months Check. 3 months or Dep. 2 months Check. 3 monthsDep. 2 months Dep. 2 months . Dep. 2 months I need embed vba so to read these cells and every time extract, that is output, the number that it is contained. 我需要嵌入vba,以便读取这些单元格,并在每次提取(即输出)包含它的编号时进行读取。

Edit: I know that there are functions when you know what you're searching for but for what I describe I am not aware of anything 编辑:我知道当您知道要搜索的内容时有功能,但是对于我描述的内容我一无所知

Can you help please as I haven't encounter a similar task in the past. 您能帮忙吗,因为我过去从未遇到过类似的任务。

You can loop through each character in the string and check to see if it is numeric. 您可以遍历字符串中的每个字符,并检查其是否为数字。 this will work for values 0-9. 这将适用于值0-9。 If there are values higher you would need to adjust this to look for multiple digits and not exit once the first one is found. 如果值更高,则需要调整它以查找多个数字,并且一旦找到第一个数字就不退出。

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

and if you have multiple digits 如果您有多个数字

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

Just to give another method: 只是给出另一种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM