VBA变体溢出

时间:2017-12-18 22:08:44

标签: vba excel-vba excel

我正在运行我在网上找到的以下功能

Function ConvertStringToNumber(str As Variant) As Long

    Dim retval As Variant
    Dim i As Integer

    retval = ""

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

    ConvertStringToNumber = retval '''''''''ERROR HERE

End Function

我收到错误Run-time error '6': Over Flow,我在网上发现它与我的系统资源有关

这些是我尝试过的输入:

  

&#34; 3aaab 23as454s24h23423ad&#34;

     

结果:溢出

     

&#34; 3aaab 23as454s24hhhhhhhhhhhhhhhhhhhhhhad&#34;

     

结果:32345424

     

&#34; ^ &安培; asdasd ^&安培; sdasd ^&安培; ^ 6776 ** ^ *&安培; ^ asdasd&#34;

     

结果:6776

     

&#34; 5asdasd 7 h4gregw56u 5 45j2dfg dfgdj46 n5k42n2&#34;

     

结果:溢出

知道为什么其中一些工作正常吗?有些不是吗?

1 个答案:

答案 0 :(得分:3)

您正在为Long变量指定一个数字。根据{{​​3}},Long可以接受最多2,147,483,647的任何数字。

如果您尝试分配号码3,234,542,423,423(即从"3aaab 23as454s24h23423ad"中取出非数字字符后的数字)或574,565,452,465,422(即数字),您将收到溢出从"5asdasd 7 h4gregw56u 5 45j2dfg dfgdj46 n5k42n2")中取出非数字字符后。