我已经使用VBA一段时间了,现在使用了多个复杂版本的if语句和for循环,但是我不知道为什么这段简单的代码无法正常工作?
弹出的错误是"Else without If"
,当我删除Else时,错误变为"End If without block If"
,如果我删除End,则错误变为"Next without for"
。我不知道为什么会这样?是因为范围d,到目前为止我对范围的使用还不够。
Sub testcode()
Dim d As Integer, i As Integer, time as Integer
Set ws = ActiveSheet
d = Range("J:J").End(xlDown).Row
For i = 3 To d
If Cells(i, 10).Value = Cells(3, 14).Value + time Then
Set newrow = ws.ListObjects("InputData")
newrow.ListRows.Add (i)
With newrow
.Range(1) = Cells(3, 10).Value
.Range(2) = Cells(3, 14).Value + time
.Range(5) = Cells(3, 15).Value
.Range(8) = Cells(3, 16).Value
.Range(9) = Not Cells(3, 17).Value
.Range(10) = Cells(3, 18).Value
Else
End If
Next i
End Sub
答案 0 :(得分:0)
Sub testcode()
Dim d As Long, i As Long, time as Long
Set ws = ActiveSheet
d = Range("J:J").End(xlDown).Row
For i = 3 To d
If Cells(i, 10).Value = Cells(3, 14).Value + time Then
Set newrow = ws.ListObjects("InputData")
newrow.ListRows.Add (i)
With newrow
.Range(1) = Cells(3, 10).Value
.Range(2) = Cells(3, 14).Value + time
.Range(5) = Cells(3, 15).Value
.Range(8) = Cells(3, 16).Value
.Range(9) = Not Cells(3, 17).Value
.Range(10) = Cells(3, 18).Value
End With
End If
Next i
End Sub
答案 1 :(得分:-1)
当您没有任何其他条件的代码时,则无需编写其他条件。 简单 如果那样的话 码... Endif