+ Reply to Thread
Results 1 to 2 of 2

Fill listbox with data from file text

  1. #1
    Pavu
    Guest

    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. #2
    Tom Ogilvy
    Guest

    RE: 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1