Importing a txt file to a ComboBox itens

  • Thread starter Thread starter mhferreira
  • Start date Start date
Loop through the file and use additem to add to the combobox.

there are several ways to access the textfile

Use File=>Open to bring it in as a separate workbook

in Excel 2000 and later, you can import it into an existing worksheet.

You can use low level fileio to read the file.

Post back in this thread with specific followup questions.
 
Try some code like the following:

Dim FName As String
Dim FNum As String
Dim S As String

FName = "H:\test.txt" '<<<< CHANGE FILE NAME
FNum = FreeFile
Open FName For Input As #FNum
Do
Input #FNum, S
Userform1.ComboBox1.AddItem S
Loop Until EOF(FNum)
Close #FNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
The following is better, as it will properly handle an empty
input file:

Dim FName As String
Dim FNum As String
Dim S As String

FName = "H:\test.txt" '<<< CHANGE FILE NAME
FNum = FreeFile
Open FName For Input As #FNum
Do Until EOF(FNum)
Input #FNum, S
Me.ComboBox1.AddItem S
Loop
Close #FNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




Chip Pearson said:
Try some code like the following:

Dim FName As String
Dim FNum As String
Dim S As String

FName = "H:\test.txt" '<<<< CHANGE FILE NAME
FNum = FreeFile
Open FName For Input As #FNum
Do
Input #FNum, S
Userform1.ComboBox1.AddItem S
Loop Until EOF(FNum)
Close #FNum


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Back
Top