将项目添加到我的 ComboBox 的开头?

Add item to the beginning of my ComboBox?

我正在尝试在 ComboBox 的顶部添加项目。

我试过这个:

iCount = 1
Worksheets("Sheet1").Activate
With Worksheets("Sheet1")
    Range("A2").Select
    Do Until IsEmpty(ActiveCell)
        If ActiveCell.Value = ComboBox1.Value And ActiveCell.Offset(0, 17) <> "" Then
            ComboBox3.AddItem
            ComboBox3.List(iCount - 1, 0) = (ActiveCell.Offset(0, 2))
            iCount = iCount + 1
        End If
        If ActiveCell.Value = "ANY" And ActiveCell.Offset(0, 17) <> "" Then
            ComboBox3.AddItem
            ComboBox3.List(iCount - 1, 0) = (ActiveCell.Offset(0, 2))
            iCount = iCount + 1
        End If
        ActiveCell.Offset(1, 0).Select
    Loop
End With

使用此代码,无论项目在 ComboBox 中的顺序如何,它们都会被添加。

我也曾尝试创建两个 Do Until 函数,但如果这样做,我的 sub 真的太长了(我的数据库中有超过 10 000 行)。

尝试 1

    ComboBox3.AddItem (ActiveCell.Offset(0, 2)), 0
    ComboBox3.List(iCount - 1, 0) = (ActiveCell.Offset(0, 2))
    ComboBox3.List(iCount - 1, 1) = Round(ActiveCell.Offset(0, 9), 2)
    ComboBox3.List(iCount - 1, 2) = (ActiveCell.Offset(0, 1))

通过这次尝试,我只会在第一列的第一行中添加 (ActiveCell.Offset(0, 2)),但是如何在第 2 列中添加其他列,例如 Round(ActiveCell.Offset(0, 9), 2)(ActiveCell.Offset(0, 1)) & 3.综上所述,我如何结合.AddItem.List

这里:

ComboBox3.AddItem ActiveCell.Offset(0, 2), 0
ComboBox3.List(0, 0) = (ActiveCell.Offset(0, 2))
ComboBox3.List(0, 1) = Round(ActiveCell.Offset(0, 9), 2)
ComboBox3.List(0, 2) = (ActiveCell.Offset(0, 1))

根据这里的 braX 评论是一个结构化的答案:

iCount = 1
Worksheets("Sheet1").Activate
With Worksheets("Sheet1")
    Range("A2").Select
    Do Until IsEmpty(ActiveCell)
        If ActiveCell.Value = ComboBox1.Value And ActiveCell.Offset(0, 17) <> "" Then
            ComboBox3.AddItem , 0
            ComboBox3.List(0, 0) = (ActiveCell.Offset(0, 2))
            ComboBox3.List(0, 1) = Round(ActiveCell.Offset(0, 9), 2)
            ComboBox3.List(0, 2) = (ActiveCell.Offset(0, 1))
            iCount = iCount + 1
        End If
        If ActiveCell.Value = "ANY" And ActiveCell.Offset(0, 17) <> "" Then
            ComboBox3.AddItem
            ComboBox3.List(iCount - 1, 0) = (ActiveCell.Offset(0, 2))
            iCount = iCount + 1
        End If
        ActiveCell.Offset(1, 0).Select
    Loop
End With

通过执行此操作,所有特定行都将添加到 ComboBox3 的顶部。