franciscofcosta
franciscofcosta

Reputation: 843

How to freeze ComboBox Excel VBA

I am writing a script on an Excel VBA UserForm. The form has three different fields that are ComboBox to be filled in by the user. I want to make the contents of ComboBox 3 dependent on what is inputted in Combobox2.

ComboBox2 is populated as follows:

  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. 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 forComboBox3 to be frozen, i.e., 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. How should I do this. Thank you.

Upvotes: 0

Views: 948

Answers (2)

franciscofcosta
franciscofcosta

Reputation: 843

As prescribed in the comments, ComboBox3 is "frozen" with ComboBox3.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.

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

Upvotes: 0

D. O.
D. O.

Reputation: 626

use "MatchRequired" of the combobox, set it to TRUE, then the user can select or type only the items existing in the combobox. no need to freeze it.

Upvotes: 1

Related Questions