VBA,另一个溢出错误

时间:2016-07-07 00:51:01

标签: vba

在下面的代码中,为什么在x = i ^ 3时会出现溢出错误?

    Dim i As Long, x As Long, s As Long, k As Byte

    Do
        i = i + 1
        x = i ^ 3

    Dim n As Long, j As Long, b As String
        b = CStr(x)
        n = Len(b)
        For j = 1 To n
            s = s + Val(Mid$(b, j, 1))
        Next

        If s = i Then
            k = k + 1
            Debug.Print i
        End If
    Loop Until k = 6

当我取出以Dim n开头的代码部分并以for循环结束并将其转换为辅助函数时,剩下的代码就可以工作(也就是说,x = i时没有溢出错误3)。

1 个答案:

答案 0 :(得分:0)

Data Type Summary (Visual Basic) - MSDN - Microsoft不是VBA VBA Data Types

中使用的数据类型

Visual Basic Long是8个字节,最小/最大值为-9,223,372,036,854,775,808到9,223,372,036,854,775,807。

VBA Long是4个字节,最小/最大值为-2,147,483,648到2,147,483,647。

  

1291 ^ 3 = 2,151,685,171'运行时错误6溢出