Excel VBA下一步没有For错误

时间:2022-11-19 23:31:12

I am getting a "Next Without for Error on the following code:

我在下面的代码中得到了“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

I assume that my "If" statement doesnt know that it's done and that the "Next" command thinks there should be another nested "for" in the "if" statement but I do not know where.

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

2 个解决方案

#1


3  

As commented, you need End IF, but I also made some slight improvements:

如评论所述,您需要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

Note: Change Sheets("Sheet1") to the appropriate sheet name.

注意:将表格(“Sheet1”)更改为相应的工作表名称。

#2


0  

The answer is in the first comment under my original question by user Johnny Mopp

答案是在用户Johnny Mopp的原始问题下的第一条评论中

You should close your If with End If. If then Else Excel VBA - "End If" needed?

您应该关闭If with End If。如果那么Else Excel VBA - “End If”需要吗?

#1


3  

As commented, you need End IF, but I also made some slight improvements:

如评论所述,您需要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

Note: Change Sheets("Sheet1") to the appropriate sheet name.

注意:将表格(“Sheet1”)更改为相应的工作表名称。

#2


0  

The answer is in the first comment under my original question by user Johnny Mopp

答案是在用户Johnny Mopp的原始问题下的第一条评论中

You should close your If with End If. If then Else Excel VBA - "End If" needed?

您应该关闭If with End If。如果那么Else Excel VBA - “End If”需要吗?