Loop and Search in TXT file

DaLiMan

Active Member
Joined
Jun 1, 2004
Messages
295
Hi There,

I hope someone can help me.
I'm trying to figure out how to loop trough 2 TXT file to search a number and then again and again.
But the problem is that the loop does not loop.....
The search in the TXT file just continues where it left of and I can't figure out how to reset this.
I'm trying to let the TXT file be left open [OPEN FOR INPUT] because I think opening and closing takes up precious time when searching for 500 or so different numbers.

Anyone any ideas?

Here a scrap of the code with the loop.

<code>
' A loop for every numbers in the list
For R = 1 To UBound(Arr, 1)

'Loop until the end of file
Do While Not EOF(intFNumber)
MsgBox Arr(R, 1)
'Read data from file
Line Input #intFNumber, sLine
If InStr(1, Mid(sLine, 1, 9), Arr(R, 1), vbTextCompare) Then
GLN = Mid(sLine, 153, 33)
MsgBox GLN, vbInformation, "GLNfabrikant + Prod.code fabrikant"

Do While Not EOF(intFNumber2)
'Read data from file
Line Input #intFNumber2, sLine2
If InStr(1, Mid(sLine2, 153, 33), GLN, vbTextCompare) Then
MsgBox (Mid(sLine2, 2, 9)), vbInformation, "TU nummer"
End If
Exit Do
Loop
End If
Exit Do

Loop
Next
'Close the file
Close #intFNumber
Close #intFNumber2

</code.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I can't find how to add attachments in this forum...?

Maybe a better explanation would help.

I'm trying to search article numbers in a TXT file and return some data behind the match.
For instance when I have a list in Excel in col.A like this.

Column A
5
9
1
2
etc

Now in a TXT file we have

1 text A
2 text B
3 text C
4 text D
5 text E
6 text F
7 text G
8 text H
9 text I
etc

Now with the VBA code above which it searches line by line in the TXT file it only finds the first 2 rows and returns [text E] and [text I].
However it does not find the numbers 1 and 2 which are lower and therefore before the others numbers in the TXT file. ( it was already passed that line number )

I like to search the TXT file from the beginning every time without opening and closing the TXT file everytime.
( I think it's slowing the execution down! )

Maybe the example code is not the right way of doing this.

Any ideas?

Thanx for the help.

Daniel
 
Upvote 0
I did not understand your goal, but you can read a textfile without opening it with this code
Code:
Sub readtextfile()
folderPath = "E:\test\"
Filename = "1.txt"
    sn = Split(CreateObject("scripting.filesystemobject").opentextfile(folderPath & Filename).readall, vbCrLf)
End Sub
then you have an array named sn with all lines
 
Upvote 0
I did not understand your goal, but you can read a textfile without opening it with this code
Code:
Sub readtextfile()
folderPath = "E:\test\"
Filename = "1.txt"
    sn = Split(CreateObject("scripting.filesystemobject").opentextfile(folderPath & Filename).readall, vbCrLf)
End Sub
then you have an array named sn with all lines

Hmm, that could be something.
Any idea how this behaves when the TXT file is 140+ mb ?
Also I would create 2 of these arrays. Could it become a memory problem?
 
Upvote 0

Forum statistics

Threads
1,223,718
Messages
6,174,082
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top