Excel VBA-带有If语句的UserForm组合框 [英] Excel VBA - UserForm Combobox with If Statement
问题描述
我已经创建了一个用户表单.该代码尚未完成,但是尝试在组合框上设置条件.这是代码:
I've created an Userform. The code is not completed yet, but trying to have conditions on the combobox. Here is the code:
Private Sub UserForm_Initialize()
With SupText
.AddItem "Sup1"
.AddItem "Sup2"
.AddItem "Sup3"
End With
With ProdText
.AddItem "Prod1"
.AddItem "Prod2"
.AddItem "Prod3"
.AddItem "Prod4"
.AddItem "Prod5"
End With
With UnitText
.AddItem "kL"
.AddItem "T"
End With
With StaText
.AddItem "In Progress"
.AddItem "Awaiting"
.AddItem ""
End With
With ProLText
.AddItem "1"
.AddItem "4"
.AddItem "1&4"
.AddItem "2"
.AddItem "3"
.AddItem "2&3"
.AddItem "WOPL"
.AddItem "BOPL"
.AddItem "Industry Line"
End With
End Sub
因此,我想做的是根据 SupText
将 ProLText
组合框填充到列表中.当我使用 if
语句时,它不起作用.例如,如果从SupText组合框中选择Sup1,我只希望提供ProLText组合框的前6个选项,则Sup2会将下一个2和Sup3赋予最后一个项目.但是由于某种原因,它会为else语句而不是if或else if提供结果.
So, what I'm trying to do is to ProLText
combobox to populate the list according to the SupText
. When I use an if
statement it doesn't work. For instance, if the choose the Sup1 from SupText Combobox I would like to give only the first 6 option of the ProLText combobox, the Sup2 gives the next 2 and Sup3 the last item. But for some reason, it gives result on for the else statement and not for if or else if.
有什么想法为什么不起作用?
Any ideas why it doesn't work?
致谢.
推荐答案
- 将组合框的
.Style
更改为fmStyleDropDownList
- 请勿在
UserForm_Initialize()
中的 - 在
SupText_Click()
事件中,清除ProLText
,然后重新添加相关项目.
ProLText
中添加项目- Change the
.Style
of the comboboxes tofmStyleDropDownList
- Do not add items to
ProLText
in theUserForm_Initialize()
- In
SupText_Click()
event clear theProLText
and re-add relevant items.
这是您要尝试的(未测试)吗?
Is this what you are trying (UNTESTED)?
Private Sub SupText_Click()
If SupText.ListIndex = -1 Then Exit Sub
ProLText.Clear
Select Case SupText.Text
Case "Sup1"
With ProLText
.AddItem "1"
.AddItem "4"
.AddItem "1&4"
.AddItem "2"
.AddItem "3"
.AddItem "2&3"
End With
Case "Sup2"
With ProLText
.AddItem "WOPL"
.AddItem "BOPL"
End With
Case "Sup3"
With ProLText
.AddItem "Industry Line"
End With
End Select
End Sub
或更短的版本
Private Sub SupText_Click()
If SupText.ListIndex = -1 Then Exit Sub
With ProLText
.Clear
Select Case SupText.Text
Case "Sup1"
.AddItem "1"
.AddItem "4"
.AddItem "1&4"
.AddItem "2"
.AddItem "3"
.AddItem "2&3"
Case "Sup2"
.AddItem "WOPL"
.AddItem "BOPL"
Case "Sup3"
.AddItem "Industry Line"
End Select
End With
End Sub
这篇关于Excel VBA-带有If语句的UserForm组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!