简体   繁体   中英

ComboBox Clear Excel VBA

I am writing a script where I have a Userform with three ComboBox . I want the contents of ComboBox3 to depend on what the user inputs in ComboBox2 . I have done so by creating an index for the contents of ComboBox2 and using the Select Case function in order to populate ComboBox3 . This is working fine.

When the UserForm is initialized, ComboBox3 is frozen until the user inputs something in ComboBox2 . However, I want ComboBox3 to become empty and re-freeze whenever the user, after inputting something in ComboBox2 , erases its contents again. I have tried to do this with the following code:

If ComboBox2 = "" Then
      ComboBox3 = "" And ComboBox3.Enabled = False
End If

This giver me a "Type Mismatch" error message. I have tried the following, and it works:

 If ComboBox2 = "" Then
          ComboBox3.Enabled = False
    End If

However, it only re-freezes ComboBox3 and I really would need it to be empty.

What am I doing wrong? Thank you very much for your help.

I think you're using And incorrectly

If you want it to be on the same line then use

If ComboBox2 = vbNullString Then
    ComboBox3 = vbNullString: ComboBox3.Enabled = False
End If

If you're not that fussed about the code format use

If ComboBox2 = vbNullString Then
    ComboBox3 = vbNullString
    ComboBox3.Enabled = False
End If

Also it's better to use vbNullString instead of empty quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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