如果我将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下拉选项的源?
答案 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