【问题标题】:Excel 2007 PC VBA -- Error 6 OverflowExcel 2007 PC VBA - 错误 6 溢出
【发布时间】:2016-11-22 04:39:47
【问题描述】:

出现错误 6 - Excel VBA 溢出。通常,这是由于一个应该很长的整数。但是这个......我只是想将三个数字相乘并将结果放在一个单元格中。不涉及任何变量。

这是错误所在的行:wsResults.Cells(4, 3).Value = (60 * 60 * 24)

我尝试关闭 Excel 并重新打开工作簿,并尝试将公式直接放入公式栏中(显示正确),但 VBA 代码仍然不起作用。

有人有什么想法吗?谢谢!

Option Explicit

'Worsheets:
Dim wbThis As ThisWorkbook
Dim wsResults As Worksheet

'Set Timekeeping Values:
Public Const GameTick As Single = (60 / 12) / 4
Public Const GameRound As Single = GameTick * 12
Public Const GameHour As Single = GameRound * 60
Public Const GameDay As Single = GameHour * 24
Public Const GameWeek As Single = GameDay * 7
Public Const GameMonth As Single = GameDay * 30
Public Const GameYear As Single = GameDay * 365

Public Sub TimeControl()

'Initialize Timer:
Randomize Timer

'Set Worksheets:
Set wbThis = ThisWorkbook
Set wsResults = wbThis.Sheets("Results")

'Clear wsResults:
wsResults.Range("A1:M1000").ClearContents

'Display Unit Names:
wsResults.Cells(1, 1).Value = "Tick"
wsResults.Cells(2, 1).Value = "Round"
wsResults.Cells(3, 1).Value = "Hour"
wsResults.Cells(4, 1).Value = "Day"
wsResults.Cells(5, 1).Value = "Week"
wsResults.Cells(6, 1).Value = "Month"
wsResults.Cells(7, 1).Value = "Year"

'Display Game Units:
wsResults.Cells(1, 2).Value = GameTick
wsResults.Cells(2, 2).Value = GameRound
wsResults.Cells(3, 2).Value = GameHour
wsResults.Cells(4, 2).Value = GameDay
wsResults.Cells(5, 2).Value = GameWeek
wsResults.Cells(6, 2).Value = GameMonth
wsResults.Cells(7, 2).Value = GameYear

'Display Real-World Units:
wsResults.Cells(1, 3).Value = ""
wsResults.Cells(2, 3).Value = 60
wsResults.Cells(3, 3).Value = (60 * 60)
wsResults.Cells(4, 3).Value = (60 * 60 * 24)
wsResults.Cells(5, 3).Value = (60 * 60 * 24 * 7)
wsResults.Cells(6, 3).Value = (60 * 60 * 24 * 30)
wsResults.Cells(7, 3).Value = (60 * 60 * 24 * 365)

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    细节

    当您尝试进行超出分配目标限制的分配时,会导致溢出。此错误有以下原因及解决方法:

  • 赋值、计算或数据类型转换的结果太大,无法在该类型变量允许的值范围内表示。

  • 将值分配给可以容纳更大范围值的类型的变量。

  • 对属性的赋值超出了该属性可以接受的最大值。


    确保您的分配适合它所针对的属性的范围。 您尝试在计算中使用数字,该数字被强制转换为整数,但结果大于整数。

    例如:

    Dim x As Long
    x = 2000 * 365   ' Error: Overflow
    

    要解决这种情况,请输入数字,如下所示:

    Dim x As Long
    x = CLng(2000) * 365
    

    在你的情况下尝试使用这个

    wsResults.Cells(4, 3).Value = CLng(60 * 60) * 24
    wsResults.Cells(5, 3).Value = CLng(60 * 60) * 24 * 7
    wsResults.Cells(6, 3).Value = CLng(60 * 60) * 24 * 30
    ...
    

    编辑

    为什么需要这种转换?

    60、60 和 24 都是整数值。在VBA 中,整数是 16 位有符号类型,当您对 2 个整数执行算术运算时,算术运算以 16 位执行。由于这两个数字相乘的结果超过了可以用 16 位表示的值,因此您会遇到异常。

    第二个示例(使用 CLng 转换)有效,因为第一个数字首先转换为 32 位类型,然后使用 32 位数字执行算术。

    在您的示例中,使用 16 位整数执行算术运算,然后将结果转换为 long,但此时为时已晚,溢出已经发生。解决方法是先将乘法中的一个操作数转换为long。

  • 【讨论】:

    • 基本上这里是类型转换函数的列表,可以在宏中使用,以避免这种溢出错误。 msdn.microsoft.com/en-us/library/s2dy91zy.aspx 。由于您一直在 excel 公式中执行此操作,因此它不是必需的,但 excel 单元格公式本身足以为您进行正确的转换。
    • 如果上述答案解决了您的问题,您可以随时将其标记为已接受,以便其他人可以将其视为已接受并已解决。谢谢!
    • 嗯,这行得通。谢谢!我以前从未遇到过。查看我的旧代码,我总是有一些计数器或其他变量在处理大值时被声明为 long。很高兴知道直接乘法会搞砸整个事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多