Visual Basic脚本excel没有返回值?

时间:2015-12-14 13:40:15

标签: vba excel-vba excel

我似乎没有发现问题。

为什么下面的代码只返回#VALUE而不是实际值?当我使用断点时,我看到retval确实只接收所需的数值。 Retval确实给“GetValues”赋值,但是当函数完成时,单元格中只出现“#VALUE”......

目标是仅继续使用特定单元格中的数值。例: 在Cell C17中有:“GPRS(2271215字节)” 然后在D18:“= GetValues(C17)”

任何想法?代码如下:

Function GetValues(ByVal s As String) As String

' Variables needed.   '
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" Then
        retval = retval + Mid(s, i, 1)
    End If
Next

' Then return the return string.                          '
GetValues = retval

Return

End Function

1 个答案:

答案 0 :(得分:0)

Function GetValues(ByVal s As String) As String

Dim retval As String   
Dim i As Integer       
Dim x As Long 'add a variable to receive the numeric value
                '
retval = ""

For i = 1 To Len(s)
    If Mid(s, i, 1) >= 0 And Mid(s, i, 1) <= 9 Then    
        retval = retval + Mid(s, i, 1)       
    End If
Next

x = CLng(retval) 'convert retval to a number
GetValues = x                    
End Function