Я пробовал этот код, но Exit не работает. Может кто-нибудь предложить исправление?

#excel #vba

#excel #vba

Вопрос:

У меня есть следующий код, и Exit for не работает в этом

 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. Какая строка не работает?

2. не работает — это не очень полезное описание. Кроме того, строка Bestslot.Cells(1 i, 6).Value = SLA_scenarios.Cells(i 1, 372 - j).Value после Exit For никогда не может быть достигнута. Если это ваша проблема, то вам нужно поставить Exit For после этой строки.

Ответ №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 .