如何冻结ComboBox Excel VBA

时间:2016-11-30 15:55:59

标签: excel vba excel-vba combobox

我正在Excel VBA UserForm上编写脚本。表单有三个不同的字段,ComboBox由用户填写。我想让ComboBox 3的内容取决于Combobox2中输入的内容。

ComboBox2填充如下:

  With ComboBox2
        .AddItem "Legal Information"
        .AddItem "Media"
        .AddItem "Official Disclosures"
        .AddItem "Patents and Trademarks"
        .AddItem "Private Corporate Information"
        .AddItem "Private Individual Information"
        .AddItem "Property Information"
        .AddItem "Public Company Information"
        .AddItem "Public Tenders"
        .AddItem "Ships, Vessels and Aircraft Information"
        .AddItem "Watchlists/Blacklists"
    End With

根据用户在ComboBox2中输入的内容,ComboBox3由不同的选项填充。我这样做如下:

Private Sub ComboBox2_Change()

Dim index As Integer
 index = ComboBox2.ListIndex

 ComboBox3.Clear

Select Case index
     Case Is = 0
         With ComboBox3
             .AddItem "Administrative"
             .AddItem "Civil"
             .AddItem "Criminal"
         End With
     Case Is = 1
         With ComboBox3
             .AddItem "Arts and Culture"
             .AddItem "Blog/Social Media"
             .AddItem "Business and Economics"
             .AddItem "General News"
             .AddItem "Intelligence and Security"
             .AddItem "Official News Agency/Official Press"
             .AddItem "Energy"
             .AddItem "Pharmaceutical and Medical News"
             .AddItem "Politics"
             .AddItem "Religion"
             .AddItem "Society, Lifestyle and Opinion"
             .AddItem "Sport"
         End With

End Sub

我希望ComboBox3被冻结,即如果选择了ComboBox2的任何其他选项,则用户无法填写Case is = 2, 3, 4, 5, 6, 7, 8, 9, 10 }。我该怎么做谢谢。

2 个答案:

答案 0 :(得分:1)

使用组合框的“MatchRequired”,将其设置为TRUE,然后用户可以选择或仅键入组合框中存在的项目。不需要冻结它。

答案 1 :(得分:0)

根据评论中的规定,ComboBox3ComboBox3.Enabled = False“冻结”。但是,为了避免用户“冻结”ComboBox然后在ComboBox2中选择其他选项的情况,有必要在每个索引案例之前插入ComboBox3.Enabled = True

更正后的代码如下。

Private Sub ComboBox2_Change()

Dim index As Integer
 index = ComboBox2.ListIndex

 ComboBox3.Clear

Select Case index
    Case Is = 0
        ComboBox3.Enabled = True
        With ComboBox3
            .AddItem "Administrative"
            .AddItem "Civil"
            .AddItem "Criminal"
        End With
    Case Is = 1
        ComboBox3.Enabled = True
        With ComboBox3
            .AddItem "Arts and Culture"
            .AddItem "Blog/Social Media"
            .AddItem "Business and Economics"
            .AddItem "General News"
            .AddItem "Intelligence and Security"
            .AddItem "Official News Agency/Official Press"
            .AddItem "Energy"
            .AddItem "Pharmaceutical and Medical News"
            .AddItem "Politics"
            .AddItem "Religion"
            .AddItem "Society, Lifestyle and Opinion"
            .AddItem "Sport"
        End With
    Case Is = 2
        ComboBox3.Enabled = False
    Case Is = 3
        ComboBox3.Enabled = False
    Case Is = 4
        ComboBox3.Enabled = False
    Case Is = 5
        ComboBox3.Enabled = False
    Case Is = 6
        ComboBox3.Enabled = False
    Case Is = 7
        ComboBox3.Enabled = False
    Case Is = 8
        ComboBox3.Enabled = False
    Case Is = 9
        ComboBox3.Enabled = False
    Case Is = 10
        ComboBox3.Enabled = False
End Select

End Sub