【问题标题】:Invalid Next Control Variable无效的下一个控制变量
【发布时间】:2021-11-17 20:11:32
【问题描述】:

我遇到了错误“编译错误:无效的下一个控制变量引用”。 如果在工作表中找不到变量,则尝试跳过代码行,例如,如果 X = 0 则跳过。无法一步步调试,直接跳转到错误。

欣赏任何 2 美分

Dim a As Integer
Dim b As Integer
Dim D As Integer

'{.......}

' a
Freight_Str_Minimum_List(1) = strFreightperMinCol:
Freight_Str_Minimum_List(2) = strFreightMinCol:
Freight_Str_Minimum_List(3) = strMOtherMinCol

' b
Freight_Col_Minimum_List(1) = FreightperMinCol:
Freight_Col_Minimum_List(2) = FreightMinCol:
Freight_Col_Minimum_List(3) = MOtherMinCol

' d
Freight_Str_PerKg_List(1) = strFreightperKgCol:
Freight_Str_PerKg_List(2) = strFreightperkgAll_InCol:
Freight_Str_PerKg_List(3) = strFreightless45All_InCol:
Freight_Str_PerKg_List(4) = strFreightgreater45All_InCol:
Freight_Str_PerKg_List(5) = strFreight100All_InCol:

' this goes until (9)

For a = 1 To 3
For b = 1 To 3
For D = 1 To 9

If Freight_Str_Minimum_List(a) = 0 Then GoTo nextsegment3
If Freight_Col_Minimum_List(b) = 0 Then GoTo nextsegment3
If Freight_Col_PerKg_List(D) = 0 Then GoTo nextsegment3


'lines of code with calculations With...end with

nextsegment3:
Next a
Next b
Next D

【问题讨论】:

  • 但调试错误并没有在该代码行中停止。在它到达 nextsegment3 之后。您可以推荐的另一种选择是什么?我希望计算不会发生,例如工作表中不存在 shipping_col_perkg_list(8)。
  • 只需使用Next 并删除abD...但是您的顺序倒过来了,应该是Dba。适当的缩进会帮助你实现这一点。
  • 反向 d, B, a 与 If Freight_Str_Minimum_List(a) = "" Then GoTo nextsegment3 是正确的解决方案。删除 a,b,d 会导致错误。感谢@BigBen 的帮助..(我们都在学习,有的是刚开始学习代码,有的是高手)
  • 如果你在 If 中使用 <> 0,你能删除 Goto 吗?
  • If Freight_Str_Minimum_List(a) 0 Then .. 那么接下来会发生什么?

标签: excel vba compiler-errors


【解决方案1】:

没有Goto:

For a = 1 To 3
    For b = 1 To 3
        For D = 1 To 9
        
            If Freight_Str_Minimum_List(a) <> 0 Then 
                If Freight_Col_Minimum_List(b) <> 0 Then 
                   If Freight_Col_PerKg_List(D) <> 0 Then 
                       'lines of code with calculations With...end with
                   End If
                End If
            End If
 
        Next D
    Next b
Next a

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多