I'm trying to add items at the top of a 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.
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).
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
?
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:
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.