+ Reply to Thread
Results 1 to 6 of 6

Import Certain Lines from a Large Text File

  1. #1
    Registered User
    Join Date
    07-18-2007
    Posts
    16

    Import Certain Lines from a Large Text File

    Hi Experts,

    I need to import certain lines from a large text file, say 79496 lines or even may become more in future. I would like to import only certain rows from the file, e.g. Line 79196 to the bottom. I tried to record a macro to get the codes, but found that the StartRow value for importing a text file could only be an integer, which means 79196 will not be a value that Excel allows. Do you have any idea how to do this? Thanks!

    p.s. Considering the appending size of this text file, I really don't want to import the file as a whole.
    Last edited by VBA Noob; 12-07-2007 at 03:55 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    If you're talking about the OpenText method, StartRow is a Variant, which in practical terms means it's a Long, which can contain numbers up to 2^31.

    Why not try it?

  3. #3
    Registered User
    Join Date
    07-18-2007
    Posts
    16
    Unfortunately it doesn't work that way. The method that I used is OpenText and it requires an integer instead of long. Attached is the screen shot of the error that I've got.
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    How about opening the file for sequential access and discard records until you get where you want?

  5. #5
    Registered User
    Join Date
    05-06-2007
    Posts
    29

    Thumbs up

    Hi blueperfume
    I think this macro will help you..
    Sub Macro2()

    Dim retstring, file_path As String
    Dim i As Long

    Set fs = CreateObject("Scripting.FileSystemObject")

    file_path = Cells(1, 3).value & "\" & Cells(2, 3).value

    If (fs.fileExists(file_path) = True) Then
    Set a = fs.OpenTextFile(file_path, 1, False)
    i = 1
    Do While a.AtEndOfStream <> True
    If (i = Cells(3, 3).value) Then
    Cells(4, 3).value = a.readline
    Exit Do
    Else
    retstring = a.readline
    End If
    i = i + 1
    Loop
    a.Close
    End If
    End Sub

    This will assume that
    Cell A3 contains whole directory path of a text file
    Cell B3 sontain file name with its extension (.txt or whatever)
    cell C3 contain the line # which you want to copy
    Then in cell D3 it will print that line #
    I hope this is what you want

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Suhas, please edit your post to add code tags. If you don't know how, consult the forum rules.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1