如何列出组合框中的项目?

How to list items in combobox?

我创建了一个 UserForm,其中包含一个空 ComboBox,用于填充用户 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

另外,如果源是范围,您可以使用 RowSource 属性.
您也可以像这样使用 List 属性:

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

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