【问题标题】:Next without For VBA接下来没有 For VBA
【发布时间】:2012-12-15 03:44:18
【问题描述】:

当我尝试运行此代码时,我不断收到“编译错误: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

【问题讨论】:

    标签: excel for-loop compiler-errors next vba


    【解决方案1】:

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

    现在,如果您的循环是必要的,我个人建议使用Select Case GSerg 指示。

    但这可能是我会做的。在单元格B9 中放置以下公式:=IF(C9=0,"",IF(C9&lt;=199999,"EP-GEARING",IF(C9&lt;=399999,"DRIVES",IF(C9&lt;=499999,"FLOW",IF(C9&lt;=599999,"SPARES",IF(C9&lt;=699999,"REPAIR",IF(C9&lt;=799999,"FS",IF(C9&lt;=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 中写出逻辑并循环遍历单元格更快、更清晰。

    【讨论】:

      【解决方案2】:

      您的代码应如下所示:

      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。

      【讨论】:

      • 这样所有条件都会被评估。 ElseIf 将在满足条件后立即停止评估。
      【解决方案3】:

      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
      

      【讨论】:

      • 不要使用 Goto。这是不必要的。
      猜你喜欢
      • 1970-01-01
      • 2013-10-05
      • 2015-02-02
      • 2019-06-08
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      相关资源
      最近更新 更多