将Excel数据转换为记事本

时间:2018-04-14 18:44:08

标签: excel vba excel-vba notepad

每当我运行宏文本文件时都会生成但每行之后都有空格。我该如何解决?

这是我的代码

Sub test()
    Dim txt As String, i As Long, rng As Range
    Set rng = Range("A1:C500")
    For i = 1 To rng.Rows.Count
        With WorksheetFunction
            txt = txt & vbCrLf & Join(.Transpose(.Transpose(rng.Rows(i).Value)), vbTab)
        End With
    Next
    Open "c:\test\test.txt" For Output As #1
        Print #1, Mid$(txt, 2)
    Close #1

End Sub

1 个答案:

答案 0 :(得分:0)

尝试Trim文字。代码应该是这样的:

txt = txt & vbCrLf & Trim(Join(.Transpose(.Transpose(rng.Rows(i).Value)), vbTab))