首页 > 解决方案 > if then ... else 语句中的重复代码

问题描述

我打算通过单击一个按钮来执行两个操作。假设动作 A 和动作 B。以下是代码:

Private Sub CommandButton2_Click()
  If "Condition A" Then
    "Action A"
    "Action B"
  Else
    "Action B"
  End If

在我的代码中,Action B 部分在“Else”语句之前和之后重复。我想知道是否有办法使代码更苗条。

标签: vbaexcel

解决方案


尝试:

If "Condition A" Then
   "Action A"
End If

"Action B" ' <-- take this outside the If

推荐阅读