VBA变体溢出

问题描述 投票:1回答:1

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

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,我在网上发现它与我的系统资源有关

这些是我尝试过的输入:

“3aaab 23as454s24h23423ad”

结果:溢出

“3aaab 23as454s24hhhhhhhhhhhhhhhhhhhhhhhad”

结果:32345424

“Asdasda ^&^&^&^ 6776 ** ^ *&^ sdasda asdasda”

结果:6776

“5asdasd 7 h4gregw56u 5 45j2dfg dfgdj46 n5k42n2”

结果:溢出

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

vba excel-vba excel
1个回答
3
投票

您正在为Long变量指定一个数字。根据the documentationLong可以接受任何数字到2,147,483,647

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

© www.soinside.com 2019 - 2024. All rights reserved.