Ошибка компиляции VBA - ожидается If или Select или Sub или Function или End of Statement - PullRequest
0 голосов
/ 04 марта 2020

Я недавно столкнулся с ошибкой компиляции VBA: «Ожидается: If или Select, Sub или Function или Свойство или Тип, С или Enum или Конец оператора»

Все мои операторы if закрыты, и я не вижу других ошибок с синтаксисом.

Вот код:

Option Explicit

Sub whileloop()

Dim intCurrentRow As Integer
Dim intLastRow As Integer
Dim strPrefix As String
Dim strSuffix As String
Dim strOperation As String
Dim snglLaborMinutes As Single



intCurrentRow = 3
intLastRow = Cells(Rows.Count, 0).End(xlUp).Row
strPrefix = Cells(2, 0).Value
strSuffix = Cells(2, 1).Value
strOperation = Cells(2, 4).Value
snglLaborMinutes = Cells(2, 3).Value

While intCurrentRow <= intLastRow
    If strPrefix = Cells(intCurrentRow, 0).Value Then
        If strSuffix = Cells(intCurrentRow, 1).Value Then
            If strOperation = Cells(intCurrentRow, 4).Value Then
                snglLaborMinutes = snglLaborMinutes + Cells(intCurrentRow, 3).Value
                Cells(intCurrentRow - 1, 3).Value = snglLaborMinutes
                Rows(1).EntireRow.Delete
                intLastRow = intLastRow - 1
                intCurrentRow = intCurrentRow - 1
            Else
                strOperation = Cells(intCurrentRow, 4).Value
            End If
        Else
             strSuffix = Cells(intCurrentRow, 1).Value
        End If
    Else
         strPrefix = Cells(intCurrentRow, 0).Value
    End If

intCurrentRow = intCurrentRow + 1

End While


End Sub

1 Ответ

0 голосов
/ 04 марта 2020

Синтаксис для While можно посмотреть здесь , но AFAIK он сохранен для обратной совместимости.

Вам лучше использовать Do L oop

While intCurrentRow <= intLastRow
  ' .... 

   intCurrentRow = intCurrentRow + 1

Wend

соответственно

Do While intCurrentRow <= intLastRow
   ' ....

    intCurrentRow = intCurrentRow + 1

Loop

И в вашем случае может быть даже лучше использовать для L oop

Dim i as long
For i = intCurrentRow To intLastRow


    ' intCurrentRow = intCurrentRow + 1  not needed any longer

Next i
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...