【问题标题】:How to solve this overflow problems如何解决这个溢出问题
【发布时间】:2018-03-12 18:12:51
【问题描述】:

请问为什么第 5 行会导致溢出?我检查了数据的 ans 将是 0 超过 0,我想这可能是问题,但我不确定,有什么解决方案可以使这项工作吗?

HKColumn = 2

Do Until Cells(6, HKColumn).Value = 0

    Cells(34, 27).Value = (Cells(6, HKColumn) - Cells(5, HKColumn)) / (Worksheets("TOTAL").Cells(6, HKColumn) - Worksheets("Total").Cells(5, HKColumn))
    Cells(34, 26).Value = "2018 " + Cells(3, HKColumn)
    Cells(34, 17).Value = (Cells(11, HKColumn) - Cells(10, HKColumn)) / (Worksheets("TOTAL").Cells(11, HKColumn) - Worksheets("Total").Cells(10, HKColumn))
    Cells(34, 16).Value = "2018 " + Cells(3, HKColumn)

    Cells(32, 27).Value = (Cells(5, HKColumn) - Cells(4, HKColumn)) / (Worksheets("TOTAL").Cells(5, HKColumn) - Worksheets("Total").Cells(4, HKColumn))
    Cells(32, 26).Value = "2017 " + Cells(3, HKColumn)
    Cells(32, 17).Value = (Cells(10, HKColumn) - Cells(9, HKColumn)) / (Worksheets("TOTAL").Cells(10, HKColumn) - Worksheets("Total").Cells(9, HKColumn))
    Cells(32, 16).Value = "2017 " + Cells(3, HKColumn)

    HKColumn = HKColumn + 1

Loop

【问题讨论】:

  • HKColumn是如何声明的?你有没有放这么长才小心
  • 第 6 行是哪一行?
  • @Vityata 我正要问这个!
  • 我确实声明为 Long Line 6: Cells(34, 17).Value = (Cells(11, HKColumn) - Cells(10, HKColumn)) / (Worksheets("TOTAL").Cells (11, HKColumn) - Worksheets("Total").Cells(10, HKColumn)) Cells(34, 16).Value = "2018 " + Cells(3, HKColumn)
  • 所以你有0/0?在 VBA 中,这给出了 6(溢出)-stackoverflow.com/questions/45485285/…

标签: vba excel


【解决方案1】:

如果所有的值都为 0,则会出现溢出,因为 0/0 在 Excel 中会引发溢出错误 - Why is 0 divided by 0 throwing an overflow error in VBA?

解决问题的两种方法:

  • 真的不推荐的方式,这会迫使 VBA 忽略每个错误 - 在代码顶部写 On Error Resume Next。然后在底部写On Error GoTo 0

  • 始终检查除数是否不等于0。这是编程的标准,因为不允许除以0。因此像这样的东西,甚至使它成为一个单独的功能:


If (Cells(6, HKColumn) - Worksheets("Total").Cells(5, HKColumn))) <> 0 Then
        Cells(34, 27).Value = (Cells(6, HKColumn) - Cells(5, HKColumn)) / the-non-zero-val
End If

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多