在嵌套循环中没有For的下一个excel VBA

时间:2013-11-17 23:27:12

标签: vba loops excel-vba nested excel

我正在处理这个代码,其中有两个嵌套循环,并且在指示的行中出现编译错误“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

1 个答案:

答案 0 :(得分: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之前结束。