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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td=bgcolor:#FFFF00]Identification_Numbers[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
20000163989​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
20000164626​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
20000164625​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
20000164840​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
20000164161​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
20000164087​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
20000164187​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
20000164197​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
20000164667​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td]
20000164263​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]
20000164913​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td]
20000164521​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td]
20000164541​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td]
20000164762​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]

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,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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