Getting an Extra Empty line when exporting Excel Range to .txt file

1.7k Views Asked by At

I am trying to copy an Excel range to a .txt file.

The export is successful, with one exception, It adds one "extra" empty line at the end.

I've read and tests many of the solution on SO (and other sites), but still without any success.

My Code (relevant part)

' === Export to the .txt file ===
Dim TxtFileName As String, lineText As String

TxtFileName = ThisWorkbook.Path & "\Inv_" & Format(Date, "yyyymmdd") & ".txt"

Open TxtFileName For Output As #1
With StockSht
    For i = 1 To LastRow
        For j = 1 To 3
            If j = 3 Then
                lineText = lineText & .Cells(i, j).Value2
            Else ' j = 1 or 2
                lineText = lineText & .Cells(i, j).Value2 & vbTab
            End If
        Next j
        Print #1, lineText
        lineText = ""
    Next i
End With
Close #1

My StockSht (worksheet object) and LastRow are defined correctly, and getting their values.

Screen-shot of the end of the exported .txt file

enter image description here

2

There are 2 best solutions below

6
On BEST ANSWER

Try using a ; on the last print line.

' === Export to the .txt file ===
Dim TxtFileName As String, lineText As String

TxtFileName = ThisWorkbook.Path & "\Inv_" & Format(Date, "yyyymmdd") & ".txt"

Open TxtFileName For Output As #1
With StockSht
    For i = 1 To LastRow
        For j = 1 To 3
            If j = 3 Then
                lineText = lineText & .Cells(i, j).Value2
            Else ' j = 1 or 2
                lineText = lineText & .Cells(i, j).Value2 & vbTab
            End If
        Next j
        If i = LastRow Then
            Print #1, lineText;
        Else
            Print #1, lineText
        End if
        lineText = ""
    Next i
End With
Close #1
2
On

You can use a semi-colon in the Print statement to control the insertion point (i.e. prevent the line-feed on the last line).

The relevant bit on the MSDN page:

Use a semicolon to position the insertion point immediately after the last character displayed.

I tested this code:

Sub PrintTest()

    Dim lng As Long

    Open "C:\foo3.txt" For Output As #1

    For lng = 1 To 10
        If lng < 10 Then
            Print #1, "foo" & lng
        Else
            Print #1, "foo" & lng; '<-- semi-colon prevents the newline
        End If
    Next lng

    Close #1

End Sub

So I would update your code like below (not tested):

' === Export to the .txt file ===
Dim TxtFileName As String, lineText As String

TxtFileName = ThisWorkbook.Path & "\Inv_" & Format(Date, "yyyymmdd") & ".txt"

Open TxtFileName For Output As #1
With StockSht
    For i = 1 To LastRow
        For j = 1 To 3
            If j = 3 Then
                lineText = lineText & .Cells(i, j).Value2
            Else ' j = 1 or 2
                lineText = lineText & .Cells(i, j).Value2 & vbTab
            End If
        Next j

        '--- new bit: check for i against LastRow and add the semicolon on last row
        If i <> LastRow Then
            Print #1, lineText
        Else
            Print #1, lineText; '<-- semi colon keeps insertion point at end of line
        End If


        lineText = ""
    Next i
End With
Close #1