0

I have a combobox on my userform that should be changed by the vba codes, but I dont want any user to be able to type values, only the vb should have this power, the user should only be able to select values.

Any ideas?

2 Answers 2

4

As suggested by Chris, add the items and then set a default value for the box, however, don't set locked, by selecting the combobox in this box select the yellow option. enter image description here

0

Just change the "Locked" property to True. It will prevent the user from changing the value: however VBA will still be able to. For example, the following code locks the ComboBox; then adds two values and selects one:

Private Sub UserForm_Initialize()
    With ComboBox1
       .Locked = True ' Prevents the user changing anything
       .AddItem "Hello"
       .AddItem "World"
       .Value = "Hello" ' Sets the value with VBA
    End with
End Sub
3
  • Will the user be able to select any of the items on the list? (Sorry for not testing, I just left the office).
    – ADrex
    Jun 9, 2016 at 21:00
  • This sets the value but user can't select (locked) check my answer
    – Sgdva
    Jun 9, 2016 at 21:22
  • If you want the user to be able to select a value, but not to free-type their own value, then just set the .Style property to 2. So instead of .Locked = True, set .Style = 2. This will restrict the user's selection to only those values which you have added via VBA. Jun 9, 2016 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.