【问题标题】:Getting an Extra Empty line when exporting Excel Range to .txt file将 Excel 范围导出到 .txt 文件时出现额外的空行
【发布时间】:2018-05-23 02:38:14
【问题描述】:

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

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

我已经阅读并测试了 SO(和其他网站)上的许多解决方案,但仍然没有任何成功。

我的代码(相关部分)

' === 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 定义正确,并获得了它们的值。

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

【问题讨论】:

  • 为什么不直接将工作表保存为txt文件并跳过逐行打印?
  • @ScottCraner 不确定我是否在关注你
  • 对不起,我帮不上忙。我假设您逐步完成了代码以确保 lastrow 是您期望的行?
  • @ScottCraner 是的,模拟太多了。此外,在 SO 和其他网站上测试了多个解决方案,但仍然没有

标签: vba excel export-to-csv export-to-text


【解决方案1】:

您可以在Print 语句中使用分号来控制插入点(即防止在最后一行换行)。

MSDN页面上的相关位:

使用分号将插入点定位在显示的最后一个字符之后。

我测试了这段代码:

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

所以我会像下面这样更新你的代码(未经测试):

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

【讨论】:

  • 这不是和我的一样吗?
  • 是的,但是当我写它时,您完全编辑了您(暂时)删除的原始答案!所以我考虑删除我的,但我认为我的答案比你的要好,因为我给出了一个经过测试的例子,说明Print 的行为和分号以及对 Shai 问题的修复。所以我很高兴离开我的,因为最小的例子在 OP 问题的范围之外很有用:)
【解决方案2】:

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

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

【讨论】:

  • 对不起,同样的结果,最后多了一个空行。你有没有测试它,我现在才面对它,因为当他试图读取一个空的最后一行时,SAP“尖叫”
  • 我有办法打开 .txt 文件,转到最后一行并按 {backspace} ?
  • 老实说,我只是用它来轻松地将数据从我的 excel 传输到使用 Bulk Insert 的现有 SQL 表中,这很好用。
  • 我会使用 powershell 而不是 vba,但这需要另一个步骤。
  • 我可能什么都知道,但我可以用谷歌搜索他们中最好的。
猜你喜欢
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
相关资源
最近更新 更多