我一直在尝试此代码,但退出无法正常工作。有人可以建议更正吗?

时间:2019-04-07 07:04:38

标签: excel vba

我有以下代码,退出不适用于此

p = Application.WorksheetFunction.Match(SLA_scenarios.Cells(i + 1, 372 - j).Value, (Capacities_sheet.Range(Capacities_sheet.Cells(1, 76), Capacities_sheet.Cells(1, 147))), False)

For m = 1 To p
    If TempBestload >= (SLA_scenarios.Cells(i + 1, 78).Value * 0.9) Then
        Exit For
        Bestslot.Cells(1 + i, 6).Value = SLA_scenarios.Cells(i + 1, 372 - j).Value
    Else:
        TempBestslot2 = Application.WorksheetFunction.VLookup(SLA_scenarios.Cells(i + 1, 2).Value, Capacities_sheet.Range("A:EQ"), (Application.Match(SLA_scenarios.Cells(i + 1, 372 - j).Value, (Capacities_sheet.Range(Capacities_sheet.Cells(1, 1), Capacities_sheet.Cells(1, 147))), False) - m), False)

        'If capacity is not zero for previous slot
        If TempBestslot2 <> 0 Then
            TempBestload = TempBestload + TempBestslot2
            Bestslot.Cells(4, 8 + a).Value = TempBestload
            Bestslot.Cells(5, 8 + a).Value = SLA_scenarios.Cells(i + 1, 372 - j).Value
            k = k + 1
            Bestslot.Cells(2, 8 + a).Value = Application.WorksheetFunction.Match(SLA_scenarios.Cells(i + 1, 2).Value, (Capacities_sheet.Range(Capacities_sheet.Cells(1, 1), Capacities_sheet.Cells(37, 1))), False)
            Bestslot.Cells(3, 8 + a).Value = 75 + Application.WorksheetFunction.Match((SLA_scenarios.Cells(1 + i, 372 - j).Value), (Capacities_sheet.Range(Capacities_sheet.Cells(1, 76), Capacities_sheet.Cells(1, 147))), False) - m + 1

        'If capacity is zero then check the next best slot
        Else:
            GoTo NextIteration2
            k = 0
        End If
    End If
Next m

1 个答案:

答案 0 :(得分:1)

您有两行永远不会运行的代码。

...
If TempBestload >= (SLA_scenarios.Cells(i + 1, 78).Value * 0.9) Then
    'This next line exits the For m = 1 To p loop entirely. The next code
    'that runs is under the Next m code line
    Exit For
    'This next line will never be run.
    Bestslot.Cells(1 + i, 6).Value = SLA_scenarios.Cells(i + 1, 372 - j).Value
Else:
...

...
    Else:
        'This next line moves execution to the NextIteration2 label. The next line
        'of code that executes is under the NextIteration2: label.
        GoTo NextIteration2
        'This next line will never be run.
        k = 0
    End If
...

如果要执行这些代码行,请将它们移到重定向代码行上方。例如将k = 0移到GoTo NextIteration2上方。

此外,在:中看不到Else:的意义。 :用于将两行代码放在同一行上。它通常由不喜欢使用垂直空间的编码人员使用,因此他们将平凡(但相关)的代码行分组为一行。例如

dim i as long: i = 10

只需删除:并使用Else

相关问题