1

我对 VBA 很陌生。我试图计算向量的中位数。以下代码不断收到有关“Block if without End if”的警告。我试图更改“End IF”的位置,但它导致另一个警告“Block end if without if”。您的意见将不胜感激。谢谢。

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")
                    Exit Do
        End If

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If

    Loop

    DoCmd.SetWarnings True

End Sub
4

2 回答 2

1

代码丢失了不止一个End If。并且还有 2 个缺失的Loop陈述。

当代码足够复杂以至于整理块结束语句变得具有挑战性时,复制该过程并基本上丢弃除块控制语句之外的所有内容。该方法将其从您当前的代码中删除。

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
        End If
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
        End If
    Loop

这是我对您需要的最佳猜测。我在其中的几个陈述中附加了评论,因为它可以帮助我正确匹配它们。

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 1
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 0
    Loop ' Not onet.EOF
于 2013-08-16T21:08:43.457 回答
1

如果在第一个代码块中退出后,您似乎错过了一个结束。那里应该有 2 个,一个用于关闭最后一个 if 语句,一个用于关闭第一个块。

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")


                   End If 'ends the If thecount = agMedian if statement -- will continue to iterate until EOF

                    Exit Do 'EOF hit.


        End If 'ends the If ag.RecordCount Mod 2 = 1 block

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If 'thecount = agMedian if statement
        End If 'end ag.RecordCount Mod 2 = 0

    Loop

    DoCmd.SetWarnings True

End Sub
于 2013-08-16T17:00:38.713 回答