循环变为无穷大并使excel崩溃

时间:2018-06-11 05:35:12

标签: excel vba crash

我目前正在处理我的VBA代码的一部分,如果某些条件得到遵守,它应该计算另一个表中不同表的总数。当我的条件不受尊重时,代码就可以了,但是当我进入条件时,带有***的行会使我的代码运行到无穷大并使excel崩溃。我检查了语法,它不应该是问题。我很难理解这行代码有什么问题。

提前感谢您的帮助!

'----------------------------------------------------
'-- Buildings' peak cooling load total calculation --
'----------------------------------------------------

Dim m As Integer, n As Integer

If Range("NP_BUILDING_TOTAL_YN") = "Yes" Then

If (Range("NP_BUILDING_TOTAL_OF") = "No" Or Range("NP_BUILDING_TOTAL_OF") = "") And (Range("NP_BUILDING_TOTAL_DF") = "No" Or Range("NP_BUILDING_TOTAL_DF") = "") Then

    For m = 1 To Sheet4.Range("NP_NBR_Y")

        For n = 1 To Sheet4.Range("NP_NBR_BUILDING")

            If Range("NP_BUILDING_TOTAL_YN" & n) = "Yes" Then

                ***Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value = Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value + Range("INPUTS_BUILDING" & n)(m + 3, 2).Value***

            End If

        Next n

    Next m

End If

If Range("NP_BUILDING_TOTAL_OF") = "Yes" And (Range("NP_BUILDING_TOTAL_DF") = "No" Or Range("NP_BUILDING_TOTAL_DF") = "") Then

    For m = 1 To Sheet4.Range("NP_NBR_Y")

        For n = 1 To Sheet4.Range("NP_NBR_BUILDING")

            If Range("NP_BUILDING_TOTAL_YN" & n) = "Yes" Then

                Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value = Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value + Range("INPUTS_BUILDING" & n)(m + 3, 2).Value * Range("INPUTS_BUILDING" & n)(m + 3, 4).Value

            End If

        Next n

    Next m

End If

If (Range("NP_BUILDING_TOTAL_OF") = "No" Or Range("NP_BUILDING_TOTAL_OF") = "") And Range("NP_BUILDING_TOTAL_DF") = "Yes" Then

    For m = 1 To Sheet4.Range("NP_NBR_Y")

        For n = 1 To Sheet4.Range("NP_NBR_BUILDING")

            If Range("NP_BUILDING_TOTAL_YN" & n) = "Yes" Then

                Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value = Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value + Range("INPUTS_BUILDING" & n)(m + 3, 2).Value * Range("INPUTS_BUILDING" & n)(m + 3, 5).Value

            End If

        Next n

    Next m

End If

If Range("NP_BUILDING_TOTAL_OF") = "Yes" And Range("NP_BUILDING_TOTAL_DF") = "Yes" Then

    For m = 1 To Sheet4.Range("NP_NBR_Y")

        For n = 1 To Sheet4.Range("NP_NBR_BUILDING")

            If Range("NP_BUILDING_TOTAL_YN" & n) = "Yes" Then

                Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value = Range("NP_BUILDING_TOTAL_TABLE")(m + 1, 2).Value + Range("INPUTS_BUILDING" & n)(m + 3, 2).Value * Range("INPUTS_BUILDING" & n)(m + 3, 4).Value * Range("INPUTS_BUILDING" & n)(m + 3, 5).Value

            End If

        Next n

    Next m

End If

End If

1 个答案:

答案 0 :(得分:1)

您正在更改Change-Event - 它会触发Change-Event的调用,您将在其中更改数据 - 这会触发Change-Event的调用(依此类推......)

为防止以递归方式触发事件例程,请将此行放在例程的顶部:

Application.EnableEvents = False

并在最后

Application.EnableEvents = True