Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Fill listbox with data from file text

Hi all
How do I fill a listbox in a userform with data from a text file?
My text file has semi colon as column delimiter, data looks like this:
Choice1;Element1;Option1
My ListBox has three columns.

I have tried the code below but I keep getting errors when trying to add
items from my list.
Could somebody help please?

Private Sub UserForm_Initialize()
dim fso, txtfile, tbl()
Dim MyList As New Collection
Set fso = CreateObject("Scripting.FilesystemObject")
Set txtfile = fso.OpenTextFile ("C:\test.txt", 1)
i = 0
Do while txtfile.AtEndOfStream < True
ReDim Preserve tbl(i)
tbl(i) = Split(txtfile.Readline,";",vbTextCompare)
MyList.Add tbl(i)
i = i +1
Loop
txtfile.Close
Set fso = Nothing
For y = 1 To MyList.Count
Listbox.AddItem MyList(y)
Next y
End Sub

TIA
Patrick


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Fill listbox with data from file text

Private Sub UserForm_Initialize()
Dim fso, txtfile, tbl, lb, s
Set fso = CreateObject("Scripting.FilesystemObject")
Set txtfile = fso.OpenTextFile("C:\test.txt", 1)
Do While txtfile.AtEndOfStream < True
s = txtfile.Readline
Debug.Print s
tbl = Split(s, ";")
Debug.Print LBound(tbl), UBound(tbl)
lb = LBound(tbl)
With ListBox1
.AddItem tbl(lb)
.List(.ListCount - 1, 1) = tbl(lb + 1)
.List(.ListCount - 1, 2) = tbl(lb + 2)
End With
Loop
txtfile.Close
Set fso = Nothing
End Sub

worked for me.

--
Regards,
Tom Ogilvy


"Pavu" wrote:

Hi all
How do I fill a listbox in a userform with data from a text file?
My text file has semi colon as column delimiter, data looks like this:
Choice1;Element1;Option1
My ListBox has three columns.

I have tried the code below but I keep getting errors when trying to add
items from my list.
Could somebody help please?

Private Sub UserForm_Initialize()
dim fso, txtfile, tbl()
Dim MyList As New Collection
Set fso = CreateObject("Scripting.FilesystemObject")
Set txtfile = fso.OpenTextFile ("C:\test.txt", 1)
i = 0
Do while txtfile.AtEndOfStream < True
ReDim Preserve tbl(i)
tbl(i) = Split(txtfile.Readline,";",vbTextCompare)
MyList.Add tbl(i)
i = i +1
Loop
txtfile.Close
Set fso = Nothing
For y = 1 To MyList.Count
Listbox.AddItem MyList(y)
Next y
End Sub

TIA
Patrick



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to fill Listbox thru VBA rash Excel Programming 6 December 20th 05 01:51 PM
Fill Data from dererent file into one file. shital shah Excel Programming 0 March 3rd 05 06:41 PM
Is there an easier way to fill this listbox D[_8_] Excel Programming 3 December 31st 04 04:38 PM
Fill a listbox with data from variable range Al Excel Programming 2 August 10th 04 07:05 AM
For..next.. help to fill listbox jasonsweeney[_69_] Excel Programming 1 April 20th 04 09:25 PM


All times are GMT +1. The time now is 03:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"