簡體   English   中英

讀取一個單元格的值並提取其包含的數字

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

特定范圍的單元格包含Check. 3 months形式的Check. 3 months Check. 3 monthsDep. 2 months Dep. 2 months 我需要嵌入vba,以便讀取這些單元格,並在每次提取(即輸出)包含它的編號時進行讀取。

編輯:我知道當您知道要搜索的內容時有功能,但是對於我描述的內容我一無所知

您能幫忙嗎,因為我過去從未遇到過類似的任務。

您可以遍歷字符串中的每個字符,並檢查其是否為數字。 這將適用於值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

只是給出另一種方法:

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