PDA

View Full Version : Solved: Loading Combo box data from .txt file.



cjyogz
07-29-2006, 11:17 PM
Hi everyone, i'd appreciate some help with this, i know it can be done because i done it a few years ago but i can't remember how i did it.

What i'm doin is using a UserForm in VBA Excel, and i want one of the Combo boxes to load its information from a .txt file saved in a folder. I neeed this to happen because the information in the Combo box will need changing every now and then.

Can anybody help?

jindon
07-30-2006, 12:04 AM
Assuming workbook and text file are in the same folder

Private Sub UserForm_Initialize()
Dim fn As String, ff As Integer, txt As String
Dim a(), i As Long
fn = ThisWorkbook.Path & Application.PathSeparator & "test.txt"
If Dir(fn) = "" Then Exit Sub
ff = FreeFile
Open fn For Input As #ff
Do While Not EOF(ff)
Line Input #ff, txt
i = i + 1
Redim Preserve a(1 To i)
a(i) = txt
Loop
Close #ff
If i > 0 Then Me.ComboBox1.List = a
End Sub

cjyogz
07-30-2006, 12:08 AM
looks like that will do the job, where does the combo box come in with the code your've supplied?

jindon
07-30-2006, 12:24 AM
oops

I made it ListBox1 instead of ComboBox1

code altered....