【问题标题】:Next without For in nested loops excel VBA [duplicate]Next没有For在嵌套循环excel VBA [重复]
【发布时间】:2013-12-01 00:02:43
【问题描述】:

我正在处理这段代码,其中有两个嵌套循环,并且在指示的行中出现编译错误“Next without for”

Sub Isindenm()

      Dim wb As Workbook
      Dim ws As Worksheet
      Dim B As Integer
      Dim firstrow As Integer
      Dim lLastRow  As Long

      lLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

      For firstrow = 14 To lLastRow

             For B = 7 To 16
                 If IsNumeric(Cells(firstrow, B)) = True Then
             Next B  ***<-------------------Compile error:"next without for"***         
                 Else
                 Cells(firstrow, 19) = Cells(firstrow, B)

                 End If

      Next firstrow


      End Sub

【问题讨论】:

  • 你不能在 next 和 if then 这样的情况下交错。将您的 if 更改为 If Not IsNumeric(Cells(firstRow, "B")) Then

标签: vba loops excel nested


【解决方案1】:

For 循环更改为:

  For firstrow = 14 To lLastRow

         For B = 7 To 16
             If IsNumeric(Cells(firstrow, B)) = True Then       
                 'Do something here if cell is numeric.
             Else
                 'Do something here if cell is not numeric.
             End If
         Next B

  Next firstrow

由于IfFor 循环内开始,它应该在调用Next firstRow 之前结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多