我正在
在以下代码块中的第一个编译错误:否则无
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
答案 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