Looping Through ComboBoxes

qaxooti

New Member
Joined
Apr 13, 2005
Messages
38
I am writing a program that will populate my combo boxes in my excel project (they are located on a UserForm). Here is some pseudo-code that explains what I want to do:

Dim i as Integer
i = 1

Open FileName For Input Access Read Shared As #FileNo
Do Until EOF(FileNo)
Line Input #FileNo, LineText
ComboBox(i).AddItem = LineText
i = i + 1
Loop
Close #FileNo

i want to iterate through all my ComboBoxes (ComboBox1, ComboBox2, etc.) and populate them with each line of the .txt file. Right now, I am having trouble doing this. I know how to do this using control arrays in VB, but I don't think you can do that in Excel, so am open for any suggestions. Help would be greatly appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

This seems to work:
Code:
Private Sub UserForm_Initialize()
Dim i As Integer, FileNo As Integer
Dim Ctl As Control
Dim LineText As String
Const FileName As String = "c:\temp\Test.txt"

FileNo = FreeFile()
i = 1

Open FileName For Input Access Read Shared As #FileNo
Do Until EOF(FileNo)
    Line Input #FileNo, LineText
    For Each Ctl In Me.Controls
        If TypeName(Ctl) = "ComboBox" Then
            Ctl.AddItem LineText
        End If
    Next Ctl
    i = i + 1
Loop
Close #FileNo

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,381
Messages
6,119,192
Members
448,874
Latest member
Lancelots

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