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

前端 未结 2 1180
挽巷
挽巷 2021-01-23 03:06

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\

2条回答
  •  天涯浪人
    2021-01-23 03:33

    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
    

提交回复
热议问题