Search in TXT file and return line to Excel

jphumani

New Member
Joined
Jan 21, 2019
Messages
18
Hi there to evereybody!

Im Juan and i'm writing from argentina.

I have a TXT file with millons o lines (literally) like this:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]22082019;01092019;30092019;20000163989;D;S;N;0,00;0,00;00;00;ETCHEVERRIGARAY JUAN CARLOS

I would like to use a command button to search for the number i've marked with bold. In addition i'd like to recieve the entire line in Excel.

Basically each line contains some tax info for argentinian members.

I've seacher the web and tried something like this:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Function buscar()
Open "D:\caba.txt" For Input As #1
Do While Not EOF(1)
Line Input #1 , dato
If dato = UserForm1.TextBox1 Then
Range("b3").Value = UserForm1.TextBox1
Range("c3").Value = dato
End Function

Wish you can help me.

Thanks in advantage

Juan Pablo
[/FONT]<strike>
</strike>
[/FONT]
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
a few decades later: Powershell
Code:
 get-content test.txt |  foreach { $_ -match "20000163989" }
 
Last edited:
Upvote 0
I'm not sure if you only want to find this number: 20000163989
The code prints the lines containing that number to the Immediate window.
Pay special attention to the comment in Red.

Code:
Option Explicit

Sub Find_20000163989()
[COLOR="#FF0000"]'Requires a reference to the Microsoft Scripting Runtime.
'Tools | References -> Microsoft Scripting Runtime[/COLOR]

Dim fso As FileSystemObject: Set fso = New FileSystemObject
Dim str_To_Find, strNextLine As String
Dim txtStream

str_To_Find = ";20000163989;"
Set txtStream = fso.OpenTextFile("c:\temp\textfile.txt", ForReading, False)

Do While Not txtStream.AtEndOfStream
    strNextLine = txtStream.ReadLine
    If InStr(1, strNextLine, str_To_Find) > 0 Then
        Debug.Print strNextLine
    End If
Loop
txtStream.Close
End Sub

Adjust the file path to your needs: c:\temp\textfile.txt
 
Last edited:
Upvote 0
Hi there!!!! Thanks for your answer.

I want to find that number, but for each line there is one unique number. How can i say...in Argentina when you receive an invoice, each customer has a sort of identification number. So what i need to do is to search that identification number and calculate the exact tax for that person. Each state gives you a LARGE txt file with millons of "identification numbers". Thats why i need something simple to track the number. And Thats also why after the search i need to copy the entiry line.

Im sorry i dont understand your line in red...

Wait for yout next answer. Thanks a lot for your help.

I'm not sure if you only want to find this number: 20000163989
The code prints the lines containing that number to the Immediate window.
Pay special attention to the comment in Red.

Code:
Option Explicit

Sub Find_20000163989()
[COLOR=#ff0000]'Requires a reference to the Microsoft Scripting Runtime.
'Tools | References -> Microsoft Scripting Runtime[/COLOR]

Dim fso As FileSystemObject: Set fso = New FileSystemObject
Dim str_To_Find, strNextLine As String
Dim txtStream

str_To_Find = ";20000163989;"
Set txtStream = fso.OpenTextFile("c:\temp\textfile.txt", ForReading, False)

Do While Not txtStream.AtEndOfStream
    strNextLine = txtStream.ReadLine
    If InStr(1, strNextLine, str_To_Find) > 0 Then
        Debug.Print strNextLine
    End If
Loop
txtStream.Close
End Sub

Adjust the file path to your needs: c:\temp\textfile.txt
 
Upvote 0
Hi,

the code of "strooman" will work, but for this amount of data it could be fairly slow. It is better to read the data at once (if there is enough RAM)

Code:
Set FSO = CreateObject("Scripting.FileSystemObject")
Tx = Split(FSO.opentextfile("z:\data.txt").readall, vbCrLf)

regards

(please test it with a few hunderts lines first)
 
Upvote 0
Hi there again! I fell a Little stupid. But where should i paste this part? I understand what you mean, but i have small experience in VBA.

So, how should i put the code?

And in the command button, should i put something? i've only put "search"

And for last, strooman said:
Sub Find_20000163989()
'Requires a reference to the Microsoft Scripting Runtime.
'Tools | References -> Microsoft Scripting Runtime

What should i put there?

Hi,

the code of "strooman" will work, but for this amount of data it could be fairly slow. It is better to read the data at once (if there is enough RAM)

Code:
Set FSO = CreateObject("Scripting.FileSystemObject")
Tx = Split(FSO.opentextfile("z:\data.txt").readall, vbCrLf)

regards

(please test it with a few hunderts lines first)
 
Upvote 0
OK, first things first. I'm not the best coder so the code below is a little bit cumbersome but I hope it get's you going.

- Open your Visual Basic Editor with the shortcut Alt+F11. When you have a laptop you probably should give the shortcut Fn+Alt+F11. The Fn button on a laptop is called the function key and is used to activate a second function on the top buttons.
- In the top menubar click Insert and then choose Module.
- Copy and Paste the VBA code wich you can find below.
- To activate the red part:

'Requires a reference to the Microsoft Scripting Runtime.
'Tools | References -> Microsoft Scripting Runtime


click in the top menubar Tools and then References. Scroll through the very long list and search for "Microsoft Scripting Runtime" check the checkbox and click OK.
- Make adjustments to the path name in this line: Set txtStream = fso_OpenTextFile("c:\temp\textfile.txt", ForReading, False). Point to the location where your text file is and you want to search.
- Return to your workbook.

Now you have to realize that we don't see your setup. How that looks like in your workbook. So we have to guess. You say: "So what i need to do is to search that identification number and calculate the exact tax for that person. Each state gives you a LARGE txt file with millons of "identification numbers".

Now let's assume that you have a bunch of your identification numbers in Column A of Sheet1 and run the code.

Excel 2016 Professional (Windows) 64 bit
A
1
Identification_Numbers
2
20000163989​
3
20000164626​
4
20000164625​
5
20000164840​
6
20000164161​
7
20000164087​
8
20000164187​
9
20000164197​
10
20000164667​
11
20000164263​
12
20000164913​
13
20000164521​
14
20000164541​
15
20000164762​
Sheet: Sheet1

Code:
Option Explicit

Sub Find_20000163989()
'Requires a reference to the Microsoft Scripting Runtime.
'Tools | References -> Microsoft Scripting Runtime

Dim fso As FileSystemObject: Set fso = New FileSystemObject
Dim str_To_Find, strNextLine As String
Dim txtStream, LastRow
Dim IdNumber, rngCell As Range
 
'Assume your id numbers in Column A. Find last empty row.
LastRow = Range("A" & Rows.Count).End(xlUp).Row

'Assign Variable IdNumber to Range
Set IdNumber = Sheets("Sheet1").Range("A2:A" & LastRow - 1)

'Cycle through every cell of the Range -> IdNumber
For Each rngCell In IdNumber
    
    'Open your file with the one million lines ! ! !
    Set txtStream = fso.OpenTextFile("c:\temp\textfile.txt", ForReading, False)
    
    'Assign the value of the cell to variable str_To_Find
    str_To_Find = rngCell.Value
    
    'Loop through the lines to find the Identification Number
    'And print the line to Immediate window
    Do While Not txtStream.AtEndOfStream
        strNextLine = txtStream.ReadLine
        If InStr(1, strNextLine, str_To_Find) > 0 Then
            Debug.Print strNextLine
            
            'If Identification Number found exit the Do Loop.
            Exit Do
        End If
    Loop
    'Close the stream
    txtStream.Close
Next
End Sub
 
Last edited:
Upvote 0
Hi,

looking for ONE number, this code should be fairly fast (if excel doesn't crash)

Code:
Const iPath As String = "c:\temp\"
Const iFile As String = "AR_Tax-code.txt"
Const Key As String = "20000163989"

Sub F_en()

'read data
Set FSO = CreateObject("Scripting.FileSystemObject")
    Tx = Split(FSO.opentextfile(iPath & iFile).readall, vbCrLf)
Set FSO = Nothing

'select lines with keyword
With CreateObject("Scripting.Dictionary")
    For i = 0 To UBound(Tx)
        If InStr(1, Tx(i), Key) > 0 Then .Add (.Count), Tx(i)
    Next i

'write in Sheet from cells(1, 1)
Cells(1, 1).Resize(.Count) = Application.Transpose(.items)

End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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