问题
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