我最近遇到了VBA编译错误:
“预期:If或Select或Sub或Function或Property或Type或With或Enum或End of statement”

我所有的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

最佳答案

虽然可以查询here,但是保留AFAIK的语法,以实现向后兼容。

您最好使用Do Loop

While intCurrentRow <= intLastRow
  ' ....

   intCurrentRow = intCurrentRow + 1

Wend

分别
Do While intCurrentRow <= intLastRow
   ' ....

    intCurrentRow = intCurrentRow + 1

Loop

在您的情况下,使用For Loop甚至更好
Dim i as long
For i = intCurrentRow To intLastRow


    ' intCurrentRow = intCurrentRow + 1  not needed any longer

Next i

关于excel - VBA编译错误-预期If或Select或Sub或Function或End语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60533582/

10-11 18:34