我如何有一个Userform事件过程(复选框单击)控制多个IF THEN语句



嘿,所以我是一个初学者与VBA,但这里是我的困境:

使用VBA创建excel用户表单-

我有一个数字1-8的组合框,显示给用户填写的文本框的数量是基于在组合框中选择的数字。

我也有一个复选框,当"TRUE"时,允许用户为文本框的输出创建自定义标题。


交货:进球数= 2
自定义标签目标?[x](真)

目标1: $1,000,000
"新家"

目标2: $50,000
"船"


我的问题是,我不能得到自定义标签框的数量是相同的目标选择的数量。因此,当目标数为2时,如果复选框为TRUE,则仍会显示8个自定义目标文本框。

我使用下面的代码找到了一种方法,但是当我输入第二个IF THEN语句时,第一个不再响应单击复选框:

Private Sub cbIMPLBL_Click()
If cboIMP.Value = "1" And cbIMPLBL.Value = "True" Then
   txtIMPLBL1.Visible = True
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
Else
   txtIMPLBL1.Visible = False
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
End If
If cboIMP.Value = "2" And cbIMPLBL.Value = "True" Then
   txtIMPLBL1.Visible = True
   txtIMPLBL2.Visible = True
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
Else
   txtIMPLBL1.Visible = False
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
End If
End Sub

这里的任何帮助将是非常感激的。因为我刚刚开始有一个不同的方式,我应该这样做,例如不写所有这些单独出来,但作为一个控制或功能(不确定术语会是什么)的某种?

非常感谢!!!!

当前两个if语句都将触发。所以这是boimp。Value = "1"则首先运行第一个if语句并隐藏第一个文本框。但是它会立即运行第二个if语句,因为它不= "2",所以它会重新隐藏文本框。

有两种选择:select case语句或else if语句。

选择案例:

If cbIMPLBL.Value = "True" Then
    Select Case cboIMP.Value
        Case 1
            'hide/unhide for 1
        Case 2
            'hide/unhide for 2
        Case Else
            'Hide all
    End Select
End If
如果其他

If cbIMPLBL.Value = "True" Then
    if cboIMP.Value = 1 then
       'do your stuff
    Else if cboIMP.Value = 2 then
       'do your stuff
    Else
       'hide everything
    end if
End If
这样,一旦代码找到一个true,它就会忽略所有其他的。如果没有找到任何true语句,则运行else。

最新更新