You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Word 365 VBA中If Else语句编译错误:End If缺失问题求解

问题解决:VBA编译错误“End if without block if”

错误原因

触发编译错误的核心原因是VBA对If-Else语句的格式有严格要求:

  • 单行If-Else语句要求Else必须和Then在同一行,不能拆分到单独一行
  • 如果要将If和Else拆分成多行书写,必须使用块结构,也就是在每个If分支末尾加上End If

另外原代码存在拼写错误:Dim child_hyp_imp_crit As Strin应修正为Dim child_hyp_imp_crit As String(缺少末尾的字母g)。

修正方案

方案1:使用单行If-Else格式

将每个If-Else合并到同一行,符合VBA单行语句语法要求:

Sub adhd_diagnosis()

Dim adult_Hyp_Imp As Integer
Dim child_Hyp_Imp As Integer
Dim adult_att_crit As String
Dim adult_hyp_imp_crit As String
Dim child_att_crit As String
Dim child_hyp_imp_crit As String ' 修正拼写错误
Dim adult_combined As String
Dim child_combined As String
adult_Hyp_Imp = (count_of_2 + count_of_3)
child_Hyp_Imp = (count_of_5 + count_of_6)

' 单行If-Else格式,Else与Then在同一行
If count_of_1 > 5 Then adult_att_crit = "meets" Else adult_att_crit = "does not meet"
If adult_Hyp_Imp > 5 Then adult_hyp_imp_crit = "meets" Else adult_hyp_imp_crit = "does not meet"
If count_of_4 > 5 Then child_att_crit = "met" Else child_att_crit = "does not meet"
If child_Hyp_Imp > 5 Then child_hyp_imp_crit = "met" Else child_hyp_imp_crit = "does not meet"
If count_of_1 > 5 And adult_Hyp_Imp > 5 Then adult_combined = "meets" Else adult_combined = "does not meet"
If count_of_4 > 5 And child_Hyp_Imp > 5 Then child_combined = "met" Else child_combined = "does not meet"

Label7.Caption = child_att_crit
Label8.Caption = child_hyp_imp_crit
Label9.Caption = child_combined
Label10.Caption = adult_att_crit
Label11.Caption = adult_hyp_imp_crit
Label12.Caption = adult_combined

End Sub

方案2:使用块结构If-Else(推荐,可读性更强)

如果希望代码结构更清晰,拆分成多行书写,必须为每个If块添加End If:

Sub adhd_diagnosis()

Dim adult_Hyp_Imp As Integer
Dim child_Hyp_Imp As Integer
Dim adult_att_crit As String
Dim adult_hyp_imp_crit As String
Dim child_att_crit As String
Dim child_hyp_imp_crit As String ' 修正拼写错误
Dim adult_combined As String
Dim child_combined As String
adult_Hyp_Imp = (count_of_2 + count_of_3)
child_Hyp_Imp = (count_of_5 + count_of_6)

' 块结构If-Else,每个分支末尾添加End If
If count_of_1 > 5 Then
    adult_att_crit = "meets"
Else
    adult_att_crit = "does not meet"
End If

If adult_Hyp_Imp > 5 Then
    adult_hyp_imp_crit = "meets"
Else
    adult_hyp_imp_crit = "does not meet"
End If

If count_of_4 > 5 Then
    child_att_crit = "met"
Else
    child_att_crit = "does not meet"
End If

If child_Hyp_Imp > 5 Then
    child_hyp_imp_crit = "met"
Else
    child_hyp_imp_crit = "does not meet"
End If

If count_of_1 > 5 And adult_Hyp_Imp > 5 Then
    adult_combined = "meets"
Else
    adult_combined = "does not meet"
End If

If count_of_4 > 5 And child_Hyp_Imp > 5 Then
    child_combined = "met"
Else
    child_combined = "does not meet"
End If

Label7.Caption = child_att_crit
Label8.Caption = child_hyp_imp_crit
Label9.Caption = child_combined
Label10.Caption = adult_att_crit
Label11.Caption = adult_hyp_imp_crit
Label12.Caption = adult_combined

End Sub

额外优化建议

可以使用IIf函数简化赋值逻辑,减少冗余代码:
例如将If count_of_1 > 5 Then adult_att_crit = "meets" Else adult_att_crit = "does not meet"替换为adult_att_crit = IIf(count_of_1 > 5, "meets", "does not meet"),其余分支同理。

内容的提问来源于stack exchange,提问作者The Mammal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:33:19