用包含#的单元格填充组合框

时间:2015-07-12 02:06:00

标签: excel-vba combobox vba excel

我试图用包含#的A列的单元格填充组合框。我收到一条错误消息:"无法找到特定对象。"请帮助我理解我在这里失踪的地方。谢谢。

        Dim i As Long
        Dim lr As Long

        Sheets("Colon").Select
        lr = Worksheets("Colon").Cells(Rows.count, "A").End(xlUp).Row

        For i = 2 To lr
            If InStr(Cells(i, 1).Value, "#") > 0 Then

                ComboBox1.List = Worksheets(1).Range("A2:A" & lr).Value
                UserForm1.Controls("ComboBox" & i).Object.List = ComboBox1.List

            End If

        Next i

1 个答案:

答案 0 :(得分:0)

在这里,我找到了一个给你。试试这个。

Public Sub fillCombo()

    Dim row, lastRow, index As Long
    Dim dataList() As String

    'Data list index
    index = 0

    'Getting the last row
    lastRow = Sheets("Colon").Cells(Rows.Count, "A").End(xlUp).row

    'Looping all row from row 2.
    For row = 2 To lastRow

        'Here, my logic is some cell value which not include "#" are not store in array.
        'If "#" is include in cell value.
        If InStr(Cells(row, 1).Value, "#") > 0 Then

            'Increase index
            index = index + 1

            'Re-declare dataList size
            ReDim Preserve dataList(index)

            'Store cell value in array
            dataList(index) = Cells(row, 1).Value

        End If

    Next row

    'Set dataList to combo box.
    Sheets("Colon").ComboBox1.List = dataList

End Sub