首页 > 解决方案 > End If without Block if - 缩进,但仍然不起作用

问题描述

我经常收到错误“End If without Block If”,尽管我有一个 End 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

标签: vba

解决方案


去掉后面的冒号Else

替换Exit ForNextExit 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

推荐阅读