如何列出combobox中的项目?

我创build了一个空的ComboBoxUserForm ,用户可以select一个制定的(基本连接)文本列表。 我得到了下面的代码,但失败了。

 Private Sub specList_change() Dim i As Integer Dim ListSpec As String 'Clear whatever in listbox Me.specList.Clear Me.lineText = "" Me.partText = "" 'Get none empty data from P6:Pxx i = 5 Do DoEvents i = i + 1 ListSpec = Sheets("SPEC CHART").Range("P" & i) 'Add data into the listbox till all data in SPEC CHART worksheet is empty If Len(ListSpec) <> 0 Then specList.AddItem (ListSpec) Loop Until ListSpec = "" End Sub 

感谢您的指导。

改用UserForm_Initialize事件。

 Private Sub UserForm_Initialize() Dim r As Range, lr As Long With Sheets("SPEC CHART") lr = .Range("P" & .Rows.Count).End(xlUp).Row If lr > 5 Then Set r = .Range("P6:P" & lr) Me.speclist.RowSource = r.Address(, , , True) End If End With End Sub 

如果源是Range,则可以使用RowSource属性。
您也可以像这样使用List属性:

 Me.speclist.List = Application.Transpose(r) 

这是代替me.speclist.RowSource = r.Address(, , , True) 。 HTH。