Excel VBA ComboBox Default Value
29,241
After adding the values, you need to set the ListIndex to 0.
Combobox1.ListIndex=0
Author by
Andrew Whittam
Updated on September 11, 2021Comments
-
Andrew Whittam over 2 years
Am looking for a way to have the combobox on my userform to have a default value of nothing or something like "Choose From Below".
Have tried various google suggestions but the value that shows in my combobox is blank if it's the first iteration of the code, or worse, the previously chosen value.
Code is below...
' Begin Code Private Sub UserForm_Initialize() Dim RngTags As Range, RngNames As Range, i As Long ComboBox1.Value = "SomeText" 'Set rng1 = Sheets("Admin").Range("deptrange2") Set rng1 = Range("ALLDEPT") With ComboBox1 .ColumnCount = 1 .Style = fmStyleDropDownList .TextAlign = fmTextAlignLeft .BoundColumn = 1 For i = 1 To rng1.Count .AddItem rng1(i).Value .List(.ListCount - 1, 1) = rng1(i).Value Next i End With End Sub ' Puts the value chosen from the list on admin f6 Private Sub ComboBox1_Change() Sheets("Admin").Range("f8").Value = ComboBox1.Value Call myUnLoad End Sub ' Gets rid of userform Sub myUnLoad() UserForm1.Hide End Sub ' End code
Thanks
Andrew
-
Andrew Whittam almost 7 yearsThanks for the responses. Where does the "ComboBox1.ListIndex = 0" go in my code? Tx
-
braX almost 7 yearsAt the end of your initialize routine.