Excel VBA下一步没有For错误

时间:2018-01-24 18:48:26

标签: excel vba excel-vba

我在以下代码中获得了“Next Without Error for Error:

sub test()
  numRows = 11
  For i = 0 To numRows
    If Cells(i + 1, 2) >= 0 Then
        Range((Cells(A, i + 1)), Cells(B, i + 1)).Select
        Selection.Copy
        Sheets("PasteLocation").Activate
        Range("Ai+1").Select
        Selection.Paste
  Next
End Sub

我假设我的“If”语句不知道它已经完成,并且“Next”命令认为在“if”语句中应该有另一个嵌套的“for”但我不知道在哪里。

2 个答案:

答案 0 :(得分:3)

如评论所述,您需要End IF,但我也做了一些小改进:

Sub test()
numRows = 11
For i = 0 To numRows
    If Cells(i + 1, 2) >= 0 Then
        With Sheets("Sheet1")
            .Range(.Cells(1, i + 1), .Cells(2, i + 1)).Copy
        End With
        Sheets("PasteLocation").Range("A" & i + 1).Paste
    End If
Next
End Sub

注意:Sheets("Sheet1")更改为相应的工作表名称。

答案 1 :(得分:0)

答案在用户Johnny Mopp

的原始问题的第一条评论中
  

你应该关闭你的If with End If。如果那么Else Excel VBA - " End If"需要的?

相关问题