Excel VBA-带有If语句的UserForm组合框 [英] Excel VBA - UserForm Combobox with If Statement

查看:45
本文介绍了Excel VBA-带有If语句的UserForm组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用户表单.该代码尚未完成,但是尝试在组合框上设置条件.这是代码:

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?

致谢.

推荐答案

  1. 将组合框的 .Style 更改为 fmStyleDropDownList
  2. 请勿在 UserForm_Initialize()
  3. 中的 ProLText 中添加项目
  4. SupText_Click()事件中,清除 ProLText ,然后重新添加相关项目.
  1. Change the .Style of the comboboxes to fmStyleDropDownList
  2. Do not add items to ProLText in the UserForm_Initialize()
  3. In SupText_Click() event clear the ProLText 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆