VBA编译错误:否则没有if

时间:2017-12-13 15:37:12

标签: vba ms-access if-statement

我正在

  

编译错误:否则无

在以下代码块中的第一个ElseIf上出现

错误。据我所知,结构是正确的,我试图删除空白和缩进,没有运气。我错过了什么?

Dim notesSQL As QueryDef
Dim Row As Integer
Dim Column, targetCell As String
Set notesSQL = db.QueryDefs("Export_Table_Notes_Filter")
Set rs = notesSQL.OpenRecordset()

If Insp_Template_No.Value = "Hazardous Area Equipment Detailed Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 16

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (count + 1)
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Visual Inspection (Ex 'd' and Ex 'e')" Then
    Column = "c"
    Row = 15

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (count + 1)
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Close Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 15

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (count + 1)
Else
    MsgBox ("Unrecognised inspection template")
End If

Loop

3 个答案:

答案 0 :(得分:2)

通过将rs循环移到If..Else..Endif之外,可以大大简化整个过程。

Dim notesSQL As QueryDef
Dim Row As Integer
Dim Column, targetCell As String
Set notesSQL = Db.QueryDefs("Export_Table_Notes_Filter")
Set rs = notesSQL.OpenRecordset()

Row = 0
If Insp_Template_No.Value = "Hazardous Area Equipment Detailed Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 16
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Visual Inspection (Ex 'd' and Ex 'e')" Then
    Column = "c"
    Row = 15
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Close Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 15
Else
    MsgBox ("Unrecognised inspection template")
End If

If Row > 0 Then
    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (Count + 1)
        rs.Movenext
    Loop
End If

答案 1 :(得分:1)

您的代码存在一些问题。您的循环设置不正确,而您缺少rs.MoveNext。试试这样:

Dim notesSQL As QueryDef
Dim Row As Integer
Dim Column, targetCell As String
Set notesSQL = Db.QueryDefs("Export_Table_Notes_Filter")
Set rs = notesSQL.OpenRecordset()

If Insp_Template_No.Value = "Hazardous Area Equipment Detailed Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 16

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (Count + 1)
        rs.Movenext
    Loop
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Visual Inspection (Ex 'd' and Ex 'e')" Then
    Column = "c"
    Row = 15

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (Count + 1)
        rs.Movenext
    Loop
ElseIf Insp_Template_No.Value = "Hazardous Area Equipment Close Inspection (Ex 'I' Ex 'iD' Ex 'nL)" Then
    Column = "b"
    Row = 15

    Do Until rs.EOF
        targetCell = (Column & Row)
        With wsheet
        .Range(targetCell).CopyFromRecordset rs
        End With

        targetCell = (Count + 1)
        rs.Movenext
    Loop
Else
    MsgBox ("Unrecognised inspection template")
End If

答案 2 :(得分:1)

整个循环是多余的,因为.CopyFromRecordset将记录集移动到.EOF。去掉它。此外,可以使用SELECT ... CASE

来改进代码
Dim notesSQL As QueryDef
Dim Row As Integer
Dim Column, targetCell As String
Set notesSQL = Db.QueryDefs("Export_Table_Notes_Filter")
Set rs = notesSQL.OpenRecordset()

Row = 0
Select Case Insp_Template_No.Value 
Case "Hazardous Area Equipment Detailed Inspection (Ex 'I' Ex 'iD' Ex 'nL)"
    Column = "b"
    Row = 16
Case "Hazardous Area Equipment Visual Inspection (Ex 'd' and Ex 'e')" 
    Column = "c"
    Row = 15
Case "Hazardous Area Equipment Close Inspection (Ex 'I' Ex 'iD' Ex 'nL)" 
    Column = "b"
    Row = 15
Case Else
    MsgBox ("Unrecognised inspection template")
End Select

If Row > 0 Then
   targetCell = (Column & Row)
   wsheet.Range(targetCell).CopyFromRecordset rs
End If