Need to loop through all items in a combobox...

hollifd

Board Regular
Joined
Apr 3, 2002
Messages
248
How do I loop through each item in a combobox using VBA and display each item using msgbox. Ultimately, I want to check to see if an item is already in a combobox before I add the item using combobox.additem

I hope it makes sense.

Thanks for any help,

David
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
David

What type of combobox is it?

One from the Control Toolbox toolbar, or the Forms toolbar, or on a userform?

What are you actually trying to populate it with?
 
Upvote 0
David,

Assuming the ComboBox is on a UserForm, try the following
Code:
Private Sub UserForm_Initialize()
    Me.ComboBox1.AddItem "a"
    Me.ComboBox1.AddItem "b"
    Me.ComboBox1.AddItem "c"
    Me.ComboBox1.AddItem "d"
End Sub

Private Sub CommandButton1_Click()
    Dim intComboItem As Integer
    
    For intComboItem = 0 To Me.ComboBox1.ListCount - 1
        MsgBox Me.ComboBox1.List(intComboItem)
    Next
End Sub
Hope this helps.
 
Upvote 0
Thanks for your time Norie,

It is a combobox on a userform. I will read a text file and add the items from the text file to the combobox. The text file has items that are repeated and I do not want to repeat the items in the combobox. I have not been able to figure out how to read the items inside the combobox so that I can check to see if the item already exists before adding it. I can do this outside of Excel if you do not have a easy alternative.

David
 
Upvote 0
David

You'll find code on the forum that does this.

2 methods I can think of

1 Import said text file to a (new?)(hidden?) worksheet and use advanced filter to get the unique values.

2 Use a Collection.

Try search to find out more info/code.

Oh, just thought of another method(s) - ADO/DAO to run a query on the text file that only returns unique values.:)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top