Prevent a .txt file file saving the last line as blank

走远了吗. 提交于 2021-02-11 12:25:06

问题


This is what I have. When this saves to .txt file I would like for the last line to be the last line of text not the open line below it. Please help! Thank you.

Sub ExportColB()
Dim myFileName As String, rng As Range, txt As String
myFileName = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
If myFileName = "" Then Exit Sub
With Range("d3", Range("d" & Rows.Count).End(xlUp))
    If .Count < 2 Then
        txt = .Value
    Else
        txt = Join(Evaluate("transpose(" & _
        .Address(external:=True) & ")"), vbCrLf)
    End If
End With
Open myFileName For Output As #1
    Print #1, txt
Close #1
End Sub

回答1:


Please try this amendment to your code.

Print #1, Left(Txt, Len(Txt) - 2)

BTW your .Count isn't a property or method of the Range object. I wonder why your code doesn't crash on this line. It needs to be reviewed.




回答2:


Instead of using Print #1, txt to write the file which will always add CRLF to the end of the file try Print #1, txt;. Do not fortget the semicolon.



来源:https://stackoverflow.com/questions/61572989/prevent-a-txt-file-file-saving-the-last-line-as-blank

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!