Import txt file to listbox VBA

Import txt file to listbox VBA

Anonymous
Not applicable
3,738 Views
5 Replies
Message 1 of 6

Import txt file to listbox VBA

Anonymous
Not applicable

Hi, I would like import ".txt" file to  "listbox"

someone knows how?

0 Likes
3,739 Views
5 Replies
Replies (5)
Message 2 of 6

OceanaPolynom
Advocate
Advocate

try this

 

    'make sure to start listbox indexes @ 0

    open "somefile.txt" for input as #1

i&=0

do
        input #1,a$

        MyListBox.AddItem (a$), (i&)
        
        i&=i&+1 

loop until eof(1)

good luck

John

Message 3 of 6

Anonymous
Not applicable

Thanks, John it's working but i need import some column. Can You show me how?

 

 

 

 

 

 

0 Likes
Message 4 of 6

OceanaPolynom
Advocate
Advocate

Sorry, I don't understand.  Do you mean a specific column in a text file.  Post an example if you can.

 

John

0 Likes
Message 5 of 6

Anonymous
Not applicable

Ok, I have more column to import and i need import all column. 

Empty columns also...

 

 

Example in attachment. 

Example have 40 column.

 

Now I use this code...

 

Dim fileName As String: fileName = "V:\CADCAM-PACK\save\test.txt"
Dim fileNo As Integer: fileNo = FreeFile
Dim r As Double: r = 0
Dim data As String: Dim col As Integer:
Dim row As Integer: row = -1
Dim rr As Integer: rr = -1
Dim EmptyList() As Double

Open fileName For Input As #fileNo
Do Until EOF(1)
Line Input #fileNo, data
row = row + 1
Loop
Close #fileNo
ReDim EmptyList(0 To row, 0 To 39) As Double
prof_men.profle_listprof.list = EmptyList

Open fileName For Input As #fileNo
Do Until EOF(1)
col = 0: rr = rr + 1: r = 0
Line Input #fileNo, data
For i = 1 To Len(data)
If Mid(data, i, 1) = Chr(9) Then If r = 0 Then prof_men.profle_listprof.list(rr, col) = Mid(data, 1, i - 1): r = i: col = col + 1: GoTo pom1:
If Mid(data, i, 1) = Chr(9) Then If col = 39 Then prof_men.profle_listprof.list(rr, col) = Mid(data, i + 1, Len(data) - (Len(data) - r)): r = 0: col = -1: Exit For
If Mid(data, i, 1) = Chr(9) Then If r <> 0 Then prof_men.profle_listprof.list(rr, col) = Mid(data, r + 1, i - r): r = i: col = col + 1:
pom1:
Next
Loop
Close #fileNo
If prof_men.Visible = False Then prof_men.show

or maybe that

 

 

Sub fromF()
Dim itab() As Variant
Dim r&, ir&, c&, dane$
Filename = "d:\demo\testlst.txt"
fileNo = FreeFile
Open Filename For Input As #fileNo
    While Not EOF(fileNo)
        Line Input #fileNo, dane
        r = r + 1
    Wend
Close #fileNo
ReDim itab(r, 0)
ir = 0: c = 0
Open Filename For Input As #fileNo
    While Not EOF(fileNo)
        Line Input #fileNo, dane
        n = UBound(Split(dane, Chr(9)))
        If n > c Then c = n
        ReDim Preserve itab(r, c)
        For i = 0 To n
            itab(ir, i) = Split(dane, Chr(9))(i)
        Next
        ir = ir + 1
    Wend
Close #fileNo
kok.lst.List = itab
End Sub

but its too slow... : |

 

0 Likes
Message 6 of 6

Scottu2
Advocate
Advocate

Hello,

I would suggest to speed up your code is to remove the ReDim Preserve itab(r,c) from the while loop.

Imagine that ReDim Preserve is VBA needs to define a new array and new size then copy every thing from the old to new.

Once or twice may be ok but every time for each line read is a lot of work.

 

 

In the previous lines the code determines the number of rows r by counting each line entry.

Do the same thing to determine the maximum columns c.

Then define size of the array itab(r, c).  ReDim Itab(r,c)

Then reopen the file and populate the array, without the ReDim Preserve function.

 

 

I hope this helps.

 

 

 

 

0 Likes