【问题标题】:fill combobox with cells that contain #用包含 # 的单元格填充组合框
【发布时间】:2015-07-12 02:05:59
【问题描述】:

我正在尝试用 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

【问题讨论】:

  • 哪行代码有问题???我猜你不能在单元格中使用“A”,你必须使用 1。
  • 另外,你有很多组合框吗? Combobox & i?听起来您正在将整个列 Range("A2:A" & lr) 添加到列表中,然后将相同的内容添加到很多列表中。也许你只需要 List.Add 和Range("A" & lr).value
  • 我收到一个错误@`UserForm1.Controls("ComboBox" & i).Object.List = ComboBox1.List 谢谢
  • 我需要将 List.Add 与Range("A" & lr).value 放在哪里?请更具体。我只有一个组合框“ComboBox1”。
  • 你有Combobox2Combobox3吗?那些是它找不到的对象

标签: excel combobox vba


【解决方案1】:

来,我给你买了一个。试试这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2018-09-19
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多