简体   繁体   English

如何冻结ComboBox Excel VBA

[英]How to freeze ComboBox Excel VBA

I am writing a script on an Excel VBA UserForm . 我在Excel VBA UserForm上编写脚本。 The form has three different fields that are ComboBox to be filled in by the user. 表单具有三个不同的字段,即用户要填写的ComboBox I want to make the contents of ComboBox 3 dependent on what is inputted in Combobox2 . 我想使ComboBox 3的内容取决于Combobox2输入的内容。

ComboBox2 is populated as follows: 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

Depending on what the user inputs in ComboBox2 , ComboBox3 is populated by different options. 根据用户在ComboBox2输入的内容, ComboBox3由不同的选项填充。 I am doing such as follows: 我这样做如下:

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

I'd like for ComboBox3 to be frozen, ie, impossible for the user to fill in, in case any other of the options of ComboBox2 is selected - in the event Case is = 2, 3, 4, 5, 6, 7, 8, 9, 10 . 我希望冻结ComboBox3 ,也就是说,如果选择了ComboBox2其他任何选项,则用户无法填写-如果Case is = 2, 3, 4, 5, 6, 7, 8, 9, 10 How should I do this. 我应该怎么做。 Thank you. 谢谢。

use "MatchRequired" of the combobox, set it to TRUE, then the user can select or type only the items existing in the combobox. 使用组合框的“ MatchRequired”,将其设置为TRUE,则用户只能选择或键入组合框中现有的项目。 no need to freeze it. 无需冻结。

As prescribed in the comments, ComboBox3 is "frozen" with ComboBox3.Enabled = False . 如注释中所述, ComboBox3ComboBox3.Enabled = False冻结。 However, in order to avoid the case where a user "freezes" the ComboBox and then selects another option in ComboBox2 , it is necessary to insert a ComboBox3.Enabled = True before each index case. 但是,为了避免用户“冻结” ComboBox ,然后在ComboBox2选择另一个选项的情况,有必要在每个索引大小写之前插入ComboBox3.Enabled = True

The corrected code is below. 更正后的代码如下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM