0

当我尝试运行此代码时,我不断收到“编译错误:next without For”。但是,在多次检查所有内容后,我看不出它如何无法识别它们的存在。这是我的第一个 VBA 代码,因此将不胜感激。

Sub Naming()
'
' Naming Macro
' Assigns a category name in a cell based on values in a cell one column over 
'

Dim number As Double

For i = 9 To 200
    number = Cells(i, 3).Value
        If number = 0 Then
            GoTo Line1
        Else
            If number <= 199999 And number > 0 Then
            Cells(i, 2) = "EP-GEARING"
        Else    
            If number <= 399999 And number > 199999 Then
            Cells(i, 2) = "DRIVES"
        Else
            If number <= 499999 And number > 399999 Then
            Cells(i, 2) = "FLOW"
        Else
            If number <= 599999 And number > 499999 Then
            Cells(i, 2) = "SPARES"
        Else
            If number <= 699999 And number > 599999 Then
            Cells(i, 2) = "REPAIR"
        Else
            If number <= 799999 And number > 699999 Then
            Cells(i, 2) = "FS"
        Else
            If number <= 899999 Then
            Cells(i, 2) = "GC-GEARING"
        Else
            GoTo Line1
Line1:
        End If
Next i

End Sub
4

3 回答 3

5

ElseIf是VB中的一个词。

If number = 0 Then
    'Do nothing
ElseIf number <= 199999 And number > 0 Then
    Cells(i, 2) = "EP-GEARING"
ElseIf number <= 399999 And number > 199999 Then
    ...
Else
    'Do nothing
End If

但是,Select Case这里更适合:

Select Case number
    Case 0
        'Do nothing
    Case 1 To 199999
        Cells(i, 2) = "EP-GEARING"
    Case 200000 To 399999
        ...
    Case Else
        'Do nothing
End Select
于 2012-12-30T19:58:42.527 回答
1

您的代码应如下所示:

Sub Naming()
'
' Naming Macro
' Assigns a category name in a cell based on values in a cell one column over 
'

Dim number As Double

For i = 9 To 200
    number = Cells(i, 3).Value

    If number <= 199999 And number > 0 Then
        Cells(i, 2) = "EP-GEARING"
    ElseIf number <= 399999 And number > 199999 Then
        Cells(i, 2) = "DRIVES"
    ElseIf number <= 499999 And number > 399999 Then
        Cells(i, 2) = "FLOW"
    ElseIf number <= 599999 And number > 499999 Then
        Cells(i, 2) = "SPARES"
    ElseIf number <= 699999 And number > 599999 Then
        Cells(i, 2) = "REPAIR"
    ElseIf number <= 799999 And number > 699999 Then
        Cells(i, 2) = "FS"
    ElseIf number <= 899999 Then
        Cells(i, 2) = "GC-GEARING"
    End If

Next i

End Sub

最初编写的代码的问题是,无论 Else 子句如何,编译器仍然期望每个 If 都有一个 End If,并且因为它们不存在而感到困惑。单个关键字最后ElseIf只需要一个End If语句。

很少建议使用 Goto。99% 的情况下,有一种更好、更简洁的方式来编写它,而无需使用 Goto。

于 2012-12-30T19:33:43.643 回答
0

其他答案表明您如何修复您的If语句,以便 VBA 识别您的ForNext配对。

现在,就个人而言,如果您的循环是必要的,我建议使用Select CaseGSerg 指示。

但这可能是我会做的。在单元格B9中放置以下公式:=IF(C9=0,"",IF(C9<=199999,"EP-GEARING",IF(C9<=399999,"DRIVES",IF(C9<=499999,"FLOW",IF(C9<=599999,"SPARES",IF(C9<=699999,"REPAIR",IF(C9<=799999,"FS",IF(C9<=899999,"GC-GEARING",""))))))))然后将其复制到您需要的地方。

或者如果你想用代码来做,你可以替换你的整个 sub 而没有循环我可以把它写成一个 1 班轮,但我希望它清晰易读:

Sub Naming()
'
' Naming Macro
' Assigns a category name in a cell based on values in a cell one column over
Dim theRange As Range
Set theRange = Range(Cells(9, 2), Cells(200, 2))
theRange.Value = "=IF(RC[1]=0,""""," & _
                "IF(RC[1]<=199999,""EP-GEARING""," & _
                "IF(RC[1]<=399999,""DRIVES""," & _
                "IF(RC[1]<=499999,""FLOW""," & _
                "IF(RC[1]<=599999,""SPARES""," & _
                "IF(RC[1]<=699999,""REPAIR""," & _
                "IF(RC[1]<=799999,""FS""," & _
                "IF(RC[1]<=899999,""GC-GEARING"",""""))))))))"
'Optional if you want only the values without the formula, uncomment next line
'theRange.Value = theRange.Value

Set theRange = Nothing

End Sub

使用 Excel 公式解决此类问题通常更快、更清晰,而不是在 VBA 中写出逻辑并循环遍历单元格。

于 2012-12-31T03:24:34.673 回答