如何从Microsoft Excel VBA中删除记事本中的额外回车

时间:2018-01-08 07:46:12

标签: excel vba excel-vba

我需要在文本中删除额外的逗号和回车符。
此代码仅删除额外的逗号:

Sub test()
Dim fn As String, txt As String
fn = Application.GetOpenFilename("TextFiles,*.txt")
If fn = "" Then Exit Sub
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
 With CreateObject("VBScript.RegExp")
    .Global = True: .MultiLine = True
    .Pattern = ",+$"
    Open Replace(fn, ".txt", "_Clean.txt") For Output As #1
        Print #1, .Replace(txt, "")
    Close #1
 End With
End Sub

如何删除文本处的额外断行/回车?

1 个答案:

答案 0 :(得分:1)

如果您只想删除文件的结束处的CR / LF,只需在;语句的末尾添加Print,即

Print #1, .Replace(txt, "");