Home » excel » Assigning values to Combo box in excel VBA

Assigning values to Combo box in excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to set the values of the combo box to 0, 1, and 2 (hard coded values and not a cell reference) but it does not seem to work. I have tried setting the RowSourceType" to 1 and toValue List`, but I am getting compile errors every time. For example, the following code does not work:

Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = "Value List"
Me.errorComboBox.RowSource = "0;1;2"
End Sub

or

Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = 1
Me.errorComboBox.RowSource = "0;1;2"
End Sub

I am getting errors on the RowSourceType line for both. How can I do this?

How to&Answers:

To statically populate the list of a combo box in Excel, try something like this:

Me.errorComboBox.List = Array("0", "1", "2")

This SO question contains links and other examples to achieve what you want.