将Excel Range导出到.txt文件时获得额外的空行 [英] Getting an Extra Empty line when exporting Excel Range to .txt file

查看:135
本文介绍了将Excel Range导出到.txt文件时获得额外的空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Excel范围复制到.txt文件.

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

导出成功,除了一个例外,它在末尾添加了一个" extra "空行.

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

我已经在 SO (和其他站点)上阅读并测试了许多解决方案,但仍然没有成功.

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

我的代码(相关部分)

' === 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

我的StockSht(工作表对象)和LastRow的定义正确,并获取它们的值.

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

导出的.txt文件末尾的屏幕截图

推荐答案

尝试在最后一个打印行上使用;.

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

这篇关于将Excel Range导出到.txt文件时获得额外的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆