Excel VBA 组合框默认值

标签 excel vba combobox default

我正在寻找一种方法,让我的用户窗体上的组合框具有默认值无或类似“从下方选择”之类的东西。

已经尝试了各种谷歌建议,但如果它是代码的第一次迭代,或者更糟糕的是之前选择的值,我的组合框中显示的值是空白的。

代码如下...

' 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

谢谢

安德鲁

最佳答案

添加值后,您需要将 ListIndex 设置为 0。
Combobox1.ListIndex=0

关于Excel VBA 组合框默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45287669/

相关文章:

c# - 防止调整大小选择聚焦 ComboBox 中的所有文本

r - 用 R 定义 Excel 的列宽

excel - Excel中匹配函数的精度错误

c# - 加载多张工作表并导出到一张 excel 工作表

VBA。比较锯齿状数组结果为 "Type Mismatch"

excel - 删除列表框上的滚动条

excel - 如何使用 Python 在 VBA 对话框上禁用或自动选择 'OK'(Application.DisplayAlerts = False 不起作用)

c# - 为什么我不能执行 ExecuteNonQuery()?

java - GWT 组合框 : Limiting number of entries displayed?

java - 在 Java 中创建 CSV 文件