结束 如果没有阻止 if - 缩进,但仍然不起作用



我经常收到错误"如果没有块 if",尽管我有一个结束如果开始 If 并且还尝试缩进语句。怎么了?

If TextBox1.Text = "" Then
MsgBox "Please Enter Value"
Else:
Sheets("confirmation").Range("F7").Value = ComboBox1.Value
Sheets("confirmation").Range("F9").Value = TextBox1.Value
For i = 20 To 1 Step -1
Rows(i & ":33").EntireRow.Hidden = False
Cells(13 + i, 1).Value = 20
i = i + 1
Exit For
UserForm1.Hide
End If

删除Else后的冒号。

Exit For替换为Next。使用Exit For可以过早退出 For 循环。

正如 @Bill Hileman 在注释中提到的,For 循环将无限运行,因为i在循环中递增,但在 For 循环体中递减。

If TextBox1.Text = "" Then
MsgBox "Please Enter Value"
Else
Sheets("confirmation").Range("F7").Value = ComboBox1.Value
Sheets("confirmation").Range("F9").Value = TextBox1.Value
For i = 20 To 1 Step -1
Rows(i & ":33").EntireRow.Hidden = False
Cells(13 + i, 1).Value = 20
i = i + 1
Next
UserForm1.Hide
End If

您还需要指示 for 循环的结束位置,并带有next。并且您需要删除:

If ... Then
....
Else
For ...
...
Exit For
Next
End if

最新更新