为什么我会收到“如果没有结束则阻止”消息?

时间:2019-02-04 14:02:01

标签: excel vba

我的代码收到错误消息,不明白为什么。我知道我必须退出两个if语句,但是这表明我没有正确执行此操作?

Sub InformatikKandidatenMitInterview()

   Dim Candidate As Range
   Dim CandidateFirstName As String
   Dim CandidateID As String
   Dim CandidateLastName As String
   Dim valuetobefound As String
   Dim i As Integer

   For Each Candidate In Sheets("Applications").Range("A2:A4000")
        If Candidate = "Informatik" Then
            CandidateFirstName = Candidate.Offset(0, 3)
            CandidateLastName = Candidate.Offset(0, 2)
            CandidateATASID = Candidate.Offset(0, 1)

        For i = 1 To 4000
            If CandidateFirstName = Worksheets("informatik").Cells(i, 2).Value And CandidateLastName = Worksheets("informatik").Cells(i, 3).Value And CandidateATASID = Worksheets("informatik").Cells(i, 2).Value Then
                Exit For

            Else
                Call select_next_empty_cell_informatik
                ActiveCell.Value = CandidateID
                ActiveCell.Offset(0, 1).Value = CandidateFirstName
                ActiveCell.Offset(0, 2).Value = CandidateLastName
                Exit For
    End If

Next

End Sub

1 个答案:

答案 0 :(得分:5)

您缺少End IfNext i

Sub InformatikKandidatenMitInterview()

    Dim Candidate As Range
    Dim CandidateFirstName As String
    Dim CandidateID As String
    Dim CandidateLastName As String
    Dim valuetobefound As String
    Dim i As Integer

    For Each Candidate In Sheets("Applications").Range("A2:A4000")
        If Candidate = "Informatik" Then
            CandidateFirstName = Candidate.Offset(0, 3)
            CandidateLastName = Candidate.Offset(0, 2)
            CandidateATASID = Candidate.Offset(0, 1)

            For i = 1 To 4000
                If CandidateFirstName = Worksheets("informatik").Cells(i, 2).Value And CandidateLastName = Worksheets("informatik").Cells(i, 3).Value And CandidateATASID = Worksheets("informatik").Cells(i, 2).Value Then
                    Exit For
                Else
                    Call select_next_empty_cell_informatik
                    ActiveCell.Value = CandidateID
                    ActiveCell.Offset(0, 1).Value = CandidateFirstName
                    ActiveCell.Offset(0, 2).Value = CandidateLastName
                    Exit For
                End If ' <-- Added
            Next i ' <-- Added
        End If

    Next Candidate

End Sub