Extract specified line from text file

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
Hey all

Is there a way that I could tell VBA to get me line 139 out of a text file? All the examples I have found on the net etc. just show how to import a whole file line by line in VBA, but ideally I would like to grab specified line numbers out of the file

Any ideas?

Patrick
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try something like this:

Code:
Sub Test()
'   *** Change to suit ***
    Const LineNo As Integer = 139
    Dim FileName As String
    Dim FileNum As Integer
    Dim r As Long
    Dim wb As Workbook
    Dim Data As String
''   *** Change path and file name to suit ***
    FileName = "C:\Temp\TheFile.txt"
    FileNum = FreeFile
    r = 1
    Set wb = Workbooks.Add
    Open FileName For Input As #FileNum
    Do While Not EOF(FileNum)
        Line Input #FileNum, Data
        If r = LineNo Then
            ActiveSheet.Cells(1, 1) = Data
            Exit Do
        End If
        r = r + 1
    Loop
    Close #FileNum
End Sub
 
Upvote 0
Hi @Andrew, I found this code very useful. But a problem occur when i'm working with a file that notepad read as 1 line. I want to only extract a string where the content is seen below:

CS@@CTACERTBRIEF 18$$
ORGID@@USFC17-2445-34.PDF$$
WSTAR

I want to only extract the orgid and the filename.
Note that file name will change and the character count is not constant.

Is their a way to do this is macro?
For your reference text file is can be downloaded HERE

Thanks.

PS. I have multiple files in my folder with same format as the one I sent as example. I want the code to read all the files and return every ORGID and filename and display it as a data in excel sheet.

Try something like this:

Rich (BB code):
Sub Test()
'   *** Change to suit ***
    Const LineNo As Integer = 139
    Dim FileName As String
    Dim FileNum As Integer
    Dim r As Long
    Dim wb As Workbook
    Dim Data As String
''   *** Change path and file name to suit ***
    FileName = "C:\Temp\TheFile.txt"
    FileNum = FreeFile
    r = 1
    Set wb = Workbooks.Add
    Open FileName For Input As #FileNum 
    Do While Not EOF(FileNum)
        Line Input #FileNum , Data
        If r = LineNo Then
            ActiveSheet.Cells(1, 1) = Data
            Exit Do
        End If
        r = r + 1
    Loop
    Close #FileNum 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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