2015年10月2日 星期五

【Excel VBA】 把數字(含小數點和,)從字串中抽出來。

'把數字從字串中抓出來
Function extractDigits(s As String) As String
    ' Variables needed (remember to use "option explicit").   '
    Dim retval As String    ' This is the return string.      '
    Dim i As Integer        ' Counter for character position. '

    ' Initialise return string to empty                       '
    retval = ""

    ' For every character in input string, copy digits to     '
    '   return string.                                        '
    For i = 1 To Len(s)
        If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Or Mid(s, i, 1) = "." Or Mid(s, i, 1) = "," Then
            retval = retval + Mid(s, i, 1)
        End If
    Next

    ' Then return the return string.                          '
    onlyDigits = retval
End Function



使用方法

result=extractDigits("ABCD13.5EFGR")

沒有留言:

張貼留言