属性“RowSource”无法在组合框上显示

时间:2016-09-03 06:57:37

标签: excel excel-vba combobox vba

如果我将ListBox的.RowSource属性设置为命名范围,则在UserForm的VBA显示和UserForm的Excel显示中都可以选择行源。但是,使用ComboBox我只在VBA显示屏上看到这个。如果在设置.RowSource属性后,我尝试.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

1 个答案:

答案 0 :(得分:1)

如果您对单个列使用NamedRange,则可以使用 ComboBox List属性:

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