简体   繁体   English

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

[英]Add item to the beginning of my ComboBox?

I'm trying to add items at the top of a ComboBox.我正在尝试在 ComboBox 的顶部添加项目。

I've tried this:我试过这个:

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

Using this code, the items are added regardless of their order in the ComboBox.使用此代码,无论项目在 ComboBox 中的顺序如何,都会添加这些项目。

I've also tried to create two Do Until functions but If I do that, my sub is really too long (I've more than 10 000 rows in my database).我还尝试创建两个Do Until函数,但如果我这样做,我的 sub 真的太长了(我的数据库中有超过 10 000 行)。

TRY 1尝试 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))

with this try, I will only add (ActiveCell.Offset(0, 2)) in the first row in the first column but how can I add the other columns like Round(ActiveCell.Offset(0, 9), 2) & (ActiveCell.Offset(0, 1)) in the column 2 & 3. To sum up, how can I combine .AddItem and .List ?通过这次尝试,我只会在第一列的第一行添加(ActiveCell.Offset(0, 2)) ,但如何添加其他列,如Round(ActiveCell.Offset(0, 9), 2) & (ActiveCell.Offset(0, 1))在第 2 列和第 3 列中。总而言之,我如何组合.AddItem.List

Here:这里:

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))

Based the braX comments here is a structured answer:基于这里的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

By doing this, all the specific rows will be added at the top of your ComboBox3 .通过这样做,所有特定行都将添加到ComboBox3的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM