嘿所以我是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这里的任何帮助将不胜感激。 因为我刚开始有一种不同的方式我应该这样做,例如不是单独写出所有这些,而是作为某种控制或功能(不确定该术语是什么)?
非常感谢!!!!
Hey so I am a beginner with VBA but here is my dilemma:
Creating an excel userform with VBA -
I have a combobox with the numbers 1-8, the number of textboxes shown for the user to fill out is based off of the number selected in the combobox.
I also have a checkbox which, when "TRUE", enables the user to create a custom title for the output of the textbox.
Ex: Number of Goals = 2 Custom Label Goals? [x] (True)
Goal 1 : $1,000,000 "New Home"
Goal 2 : $50,000 "Boat"
My issue is that I cannot get the number of custom label boxes to be the same as the number of goals selected. So when Number of goals is 2, 8 custom goal textboxes still show up if the checkbox is TRUE.
I found a way using the code below, but when I enter the second IF THEN statement, the first no longer responds to the clicking the checkbox:
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 SubAny help here would be greatly appreciated. As I am just beginning is there a different way I am supposed to be doing this, e.g. not writing all of this out individually, but as a control or function (not sure what the term would be) of some sort?
Thank you so much!!!!
最满意答案
目前两个if语句都将触发。 因此,如果cboIMP.Value =“1”,则它首先运行第一个if语句并取消隐藏第一个文本框。 但它会立即运行第二个if语句,因为它不是“2”,它会隐藏文本框。
选择case语句或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这样一旦代码找到了真值,它就会忽略所有其他代码。 如果它没有找到任何真正的语句,则运行else。
Currently both if statements will fire. So if cboIMP.Value = "1" then it first runs through the first if statement and unhides the first text box. But it immediately runs the second if statement and because it does not = "2" it re hides the text box.
There are two choices a select case statement or else if statements.
Select Case:
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 IfElse 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 IfThis way once the code finds a true it ignores all others. If it does not find any true statements it runs the else.
更多推荐
发布评论