Importing a txt file to a ComboBox itens

T

Tom Ogilvy

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.
 
C

Chip Pearson

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
 
C

Chip Pearson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top