简体   繁体   English

如何从 Excel 工作表中导出 txt 文件而末尾没有空行?

[英]How can I export a txt file from an Excel sheet without having a blank line at the end?

When I export a sheet as txt it generates an empty line at the end.当我将工作表导出为 txt 时,它会在末尾生成一个空行。
不正确的txt文件

What I need我需要的
在此处输入图像描述

I use this code to detect the used range and only export that.我使用这段代码来检测使用的范围并只导出它。 It is necessary to have the workbook with the data to copy open.必须打开包含要复制的数据的工作簿。

Sub export_range_txt()
    
    Workbooks.Add
    y = ActiveWorkbook.Name
    'insert the name of the workbook were data is been copied and create  this sub there
    Windows("original.xlsm").Activate 
    ActiveSheet.Activate
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row 
    Range("A1:Y" & LastRow).Copy
    Windows(y).Activate
    ActiveSheet.Activate
    Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
    Application.DisplayAlerts = False

    'change path to desired location of the txt file
    ActiveWorkbook.SaveAs Filename:= _
      "path\test.txt", FileFormat:=xlText, _
      CreateBackup:=False

    ActiveWindow.Close
    Application.DisplayAlerts = True

End Sub

The following function will export a range to a text file without a blank line at the end.以下 function 会将范围导出到文本文件,最后没有空行。

Function exportRgToTxt(rg As Range, filename As String)

    ' use a semicolon as a column separator, adjust accordingly or use a parameter
    Const SEPARATOR = ";"

    Dim i As Long, j As Long
    Dim vdat As Variant, vRow As Variant
        
    ' Placing the values of the range into an array
    vdat = rg.Value

    Dim txtFile As Long
    txtFile = FreeFile
    Open filename For Output As txtFile
        
    ' Write each row of the range to the text file but the last one
    For i = LBound(vdat, 1) To UBound(vdat, 1) - 1
        vRow = Application.WorksheetFunction.Index(vdat, i, 0)  ' Get the i-th row of the array
        vRow = Join(vRow, SEPARATOR)
        Print #txtFile, vRow   ' This will add a CRLF at the end of the line
    Next i
    
    ' Write Last row without an CRLF
    vRow = Application.WorksheetFunction.Index(vdat, UBound(vdat, 1), 0)
    vRow = Join(vRow, SEPARATOR)
    Print #txtFile, vRow; ' the semicolon will avoid the CRLF at the end of the file
    Close txtFile

End Function

Be aware, the function will fail in case the range contains a single cell only.请注意,如果范围仅包含单个单元格,则 function 将失败。 One could adjust it but I leave that to the reader.可以调整它,但我将其留给读者。

That's how you can test it这就是你可以测试它的方式

Sub testit()
    exportRgToTxt Range("A1").CurrentRegion, "D:\tmp\abc.txt"
End Sub

Further reading on the Print Statement .进一步阅读打印声明 Especially the charpos parameter is the one we need here特别是charpos参数是我们这里需要的

charpos夏波斯
Specifies the insertion point for the next character.指定下一个字符的插入点。 Use a semicolon to position the insertion point immediately after the last character displayed.使用分号 position 插入点紧跟在最后显示的字符之后。 Use Tab(n) to position the insertion point to an absolute column number.使用 Tab(n) 将插入点 position 指向绝对列号。 Use Tab with no argument to position the insertion point at the beginning of the next print zone.使用不带参数的 Tab 到 position 下一个打印区域开头的插入点。 If charpos is omitted, the next character is printed on the next line .如果省略 charpos,则下一个字符打印在下一行

See below how one could use the function in the OP's code请参阅下面如何在 OP 代码中使用 function

Sub export_range_txt()
    
    Workbooks.Add
    y = ActiveWorkbook.Name
    'insert the name of the workbook were data is been copied and create  this sub there
    Windows("original.xlsm").Activate
    ActiveSheet.Activate
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    
    'Range("A1:Y" & LastRow).Copy  ' this line is not needed any longer
    
    ' Here you could use the exportRgToTxt instead
    exportRgToTxt Range("A1:Y" & LastRow), "<your file name>"
    
    
    ' the remaining code is not neccessary
'    Windows(y).Activate
'    ActiveSheet.Activate
'    Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'
'
'
'    Application.DisplayAlerts = False
'    ActiveWorkbook.SaveAs Filename:= _
'   'change path to desired location of the txt file
'        "path\test.txt", FileFormat:=xlText, _
'    CreateBackup:=False
'    ActiveWindow.Close
'    Application.DisplayAlerts = True

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从excel导出的txt文件中删除“”标记? - How can I delete " mark from txt file I export from excel? 如何将 Excel 数据表作为分隔符冒号导出到 a.txt 文件? - How do I export an Excel data sheet to a .txt file as Delimiter colon? 如何从Excel工作表中导出数据以创建以特定格式排列的单元格数据的文本文件? - How can I export data from an excel sheet to create a text file of cell data arranged into a specific format? VBA-将Excel工作表导出到.txt文件 - VBA - Export excel sheet to a .txt file 如何使用PowerShell将txt文件中每行的第一个单词放入Excel工作表中? - How do I take the first word from each line in txt file and put them into an Excel sheet using PowerShell? 使用 openrowset 时,如何避免在 excel 表的末尾插入空行 - When using openrowset how can I avoid inserting blank lines at the end of an excel sheet 当我从Excel工作表中写入内容时,如何避免在文本文件中使用换行符? - How can I avoid Line feeder in text file when I write the content from excel sheet? 如何从 a.txt 文件中获取文本并将其写入同一个 excel 文件的新工作表中 - How do I get the text from a .txt file and write it in a new sheet in the same excel file 如何在没有SPSS代码的情况下将SPSS自定义表导出到特定的Excel工作表? - How can I export SPSS custom tables to a specific Excel sheet without the SPSS code? 如何将Excel中的行范围复制到txt文件 - How can I copy a range of rows from excel to txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM