Share via

delete line # / text file / vba exel

Anonymous
2016-04-11T23:25:59+00:00

I have a huge text file where the two first lines are relatively short but the third one is very large (29077437 chars).  Therefore, to save time,

I wonder if there is a function in VBA Excel which permit us to delete, let say the second line and to save it without having to read the third line?

ALP

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Anonymous
    2016-04-13T04:49:21+00:00

    I suppose you could do this with a combination of Line Input# and Print#?

    That wouldn't really modify the original text file, but you could copy the lines that you would like to keep, (based on the criteria that you specify) and output the data to a new text file.

    https://msdn.microsoft.com/en-us/library/office/gg264130.aspx

    Data read with Line Input # is usually written from a file with Print #.

    The Line Input # statement reads from a file one character at a time until it encounters a carriage return (Chr(13)) or carriage return–linefeed (Chr(13) + Chr(10)) sequence. Carriage return–linefeed sequences are skipped rather than appended to the character string.

    =======================================

    Something like this?

    (Warning: "Aircode")

    IF this text file is ALWAYS going to be 3 lines as you describe above:

    Dim i As Integer

    i=0

    Dim TextLine 
    Open "Readfile" For Input As #1 ' Open file.Do While Not EOF(1) ' Loop until end of file. i = i + 1If i <> 2 Then 'You said you wanted to ship the second line
     Line Input #1, TextLine ' Read line into variable.End IfOpen "WriteFile" For Output as #2Print# TextLineClose #2
     
    Loop 
    Close #1 ' Close file. 
    

    Edit:

    This might be worth a read as well?

    http://software-solutions-online.com/2014/03/15/vba-modify-existing-text-file/

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-04-13T03:38:23+00:00

    Yes you are wright.  My question is not clear.  It was in my head but not on the web.

    So as I have mentioned, in a huge text file, the two first lines are short but the thirds one is very huge.

    I have try to read the first and the last line (discard the second one) then saved the file but it is very time consuming (few minutes).  It is too long.

    So I was looking for a function which will permits me to open a text file, delete the second line thereafter saving this file.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-04-13T03:17:03+00:00

    I have a huge text file where the two first lines are relatively short but the third one is very large (29077437 chars).  Therefore, to save time,

    I wonder if there is a function in VBA Excel which permit us to delete, let say the second line and to save it without having to read the third line?

    You can certainly read the file line by line, using the good old Basic I/O statement Line Input.  So you could read the first two lines and close the file. I don't know what you mean by "delete the second line", but reading is straightforward enough.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-04-11T23:35:12+00:00

    I am interested in both.  The VBA macro in Excel as well as in Access

    Was this answer helpful?

    0 comments No comments
  5. ScottGem 68,830 Reputation points Volunteer Moderator
    2016-04-11T23:29:23+00:00

    You posted in the Access topic. I suggest you repost under the Excel topic.

    Was this answer helpful?

    0 comments No comments