Excel VBA 组合框默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45287669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA ComboBox Default Value
提问by Andrew Whittam
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.
尝试了各种 google 建议,但如果它是代码的第一次迭代,或者更糟的是之前选择的值,则在我的组合框中显示的值是空白的。
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
安德鲁
回答by braX
After adding the values, you need to set the ListIndex to 0.
添加值后,您需要将 ListIndex 设置为 0。
Combobox1.ListIndex=0
Combobox1.ListIndex=0