Excel VBA错误:编译错误下一步没有For

时间:2017-11-01 10:17:28

标签: vba

以下是我的代码。当我运行它时,它会显示:

  

编译错误下一步没有For。

请帮忙!

Sub W09T1()
Dim i As Integer
Dim FinalRow As Integer
Dim thisCategory As String, thisProduct As String
Dim thisQty As Integer
Dim Sale As Boolean
Dim discount As Single

With Sheets("sheet1")
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

Range("D1").Value = "Discount"

For i = 2 To FinalRow
    thisCategory = Cells(i, 1).Value
    thisProduct = Cells(i, 2).Value
    thisQty = Cells(i, 3).Value

    'insert select case to determine if the product
    'is on sale here


    'Use If-Then-Else and/or Select Case to
    'determine the discount based on product
    'category and discount rules
    If Sale Then
        discount = 0.25
    Else
       If thisCategory = "Fruit" Then
            Select Case thisQty
                Case Is < 5
                    discount = 0
                Case 5 To 20
                    discount = 0.1
                Case Is > 20
                    discount = 0.15
            End Select
        ElseIf thisCategory = "Herbs" Then
            'insert select case to determine
            'discount of Herbs here
        ElseIf thisCategory = "Vegetables" Then
            'insert If-Then-Else and/or Select
            'Case to determine discount of
            'Vegetables

    'insert discount to be displayed in column D


    If Sale Then
        Cells(i, 1).Resize(1, 4).Interior.Color = vbYellow
    End If
Next i
End With

End Sub

1 个答案:

答案 0 :(得分:0)

你错过了2 End If

有时当你错过了

  • 结束如果

  • 结束

  • 结束循环

For..Next循环中,VBA对哪个问题确实存在问题并且错误地描述了错误。

您的所有IfElseIf'都需要End If。就个人而言,我避免使用ElseIf,因为与使用漂亮的If..Then..Else..End If块(根据需要嵌套)相比,它们使代码更难以遵循。

您的代码:

我用第二个If-Else-Elseif-Elseif声明替换了Select..Case内容:

Sub W09T1()

    Dim i As Integer
    Dim FinalRow As Integer
    Dim thisCategory As String, thisProduct As String
    Dim thisQty As Integer
    Dim Sale As Boolean
    Dim discount As Single

    With Sheets("sheet1")
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

    Range("D1").Value = "Discount"

    For i = 2 To FinalRow
        thisCategory = Cells(i, 1).Value
        thisProduct = Cells(i, 2).Value
        thisQty = Cells(i, 3).Value

        'insert select case to determine if the product
        'is on sale here

        'Use If-Then-Else and/or Select Case to
        'determine the discount based on product
        'category and discount rules
        If Sale Then
            discount = 0.25
        Else
            Select Case thisCategory
                Case "Fruit"
                    Select Case thisQty
                        Case Is < 5
                            discount = 0
                        Case 5 To 20
                            discount = 0.1
                        Case Is > 20
                            discount = 0.15
                    End Select
                Case "Herbs"
                    'insert select case to determine
                    'discount of Herbs here
                Case "Vegetables"
                    'insert If-Then-Else and/or Select
                    'Case to determine discount of
                    'Vegetables
                    'insert discount to be displayed in column D
            Case Else
                'you could optionally add code here that will run
                ' if "thisCategory" isn't Fruit,Herbs or Vegetables
            End Select
        End If
        If Sale Then
            Cells(i, 1).Resize(1, 4).Interior.Color = vbYellow
        End If
    Next i
    End With
End Sub