excel - 属性 "RowSource"无法在组合框中显示

标签 excel combobox vba

如果我设置 .RowSource ListBox 的属性到 命名范围 然后在用户窗体的 VBA 显示和用户窗体的 Excel 显示中都可以选择行源。但是,使用 ComboBox 时,我只能在 VBA 显示屏上看到它。如果在设置 .RowSource 之后我尝试的属性(property).AddItem到我得到的 ComboBox 对象 run time error 70, permission denied .

Private Sub UserForm_Initialize()
    With pres_unit
        .AddItem "kPa"
        .AddItem "bar"
    End With
End Sub

如何使用命名范围作为 ComboBox 下拉选项的来源?

Excel screenshot of UserForm

VBA screenshot of UserForm

最佳答案

如果您使用的是 NamedRange对于单个列,您可以使用 List 的属性(property)组合框 :

Private Sub UserForm_Initialize()      

' create the Named Range "myNameRange" 
' you can manually (or with variables) modify the Range("B2:B10") in "Sheet1"
ThisWorkbook.Names.Add "myNameRange", Sheets("Sheet1").Range("B2:B10")

With pres_unit
    .List = Range("myNameRange").value
    'disallows user input, only values from list
    .Style = fmStyleDropDownList
End With

End Sub

关于excel - 属性 "RowSource"无法在组合框中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304165/

相关文章:

.net - 如何在 Excel 中表达 NaN 值?

python - 如何将四列excel数据作为pandas中的两行

c# - 清除 ComboBox 选中的文本

arrays - 如何用在module1 VBA中填充的数组填充列表框

c# - 连接到网络上的 Excel 工作表(数据库)

vba - 如何使用vba宏从指定单元格开始计算excel中的行数

vba - 使用单个列表项创建 LIST 验证

c# - 在 C# 中以编程方式声明组合框时,无法在初始状态设置 selectedValue

c# - 如何将带有转换器的组合框绑定(bind)到 WPF 中的列表

arrays - 无法使用数组成员变量分配数组