预期的结束子和控制变量已在使用错误

时间:2017-08-13 18:08:06

标签: excel vba excel-vba

当我尝试调试此代码时,我遇到两个错误,第一个错误说" For控制变量已经在使用",第二个"预期结束Sub"。我的代码的目标是我在我的工作表中创建了一系列支票" poteaux"我想从表格中获取价值"努力poteaux"并将它们放在poteaux的某些细胞中,以便检查配方。 msg框提示我是否好。我是vba的新手,我试试看。

Private Sub CommandButton1_Click()
    For i = 4 To 15
        Sheets("Poteaux").Range("C4").Value = Sheets("Efforts poteaux").Cells(i, 6)
        Sheets("Poteaux").Range("D4").Value = Sheets("Efforts poteaux").Cells(i, 11)
        If Sheets("Poteaux").Value("I4") = "NG" Then
            Sub Msg_exe()
            MsgBox "NG"
            End Sub
            Exit For
        End If
    Next i
    MsgBox "OK"
End Sub

2 个答案:

答案 0 :(得分:0)

Private Sub CommandButton1_Click()
For i = 4 To 15
Sheets("Poteaux").Range("C4").Value = Sheets("Efforts poteaux").Cells(i, 6)
Sheets("Poteaux").Range("D4").Value = Sheets("Efforts poteaux").Cells(i, 11)
If Sheets("Poteaux").Range("I4") = "NG" Then
    MsgBox "NG on " & i
    Exit Sub
End If
Next i
MsgBox "OK"
End Sub

答案 1 :(得分:0)

不要尝试嵌套子程序。对它们进行独立编码,然后从另一个调用

Private Sub CommandButton1_Click()
    dim i as long

    For i = 4 To 15
        workSheets("Poteaux").Range("C4") = workSheets("Efforts poteaux").Cells(i, 6).Value
        workSheets("Poteaux").Range("D4") = workSheets("Efforts poteaux").Cells(i, 11).Value
        If workSheets("Poteaux").RANGE("I4") = "NG" Then
            Msg_exe i
            Exit For
        End If
    Next i
    MsgBox "OK"
End Sub

Sub Msg_exe(i as long)
    MsgBox "NG on " & i
End Sub