Excel在输出文件的末尾添加空行

时间:2016-11-30 23:13:18

标签: excel vba

我有一个很小但很烦人的问题。我编写了一个代码来分析一定数量的数据并以单元格式绘制结果。最后,结果应转换为文件格式。这是我导出到文件:

Sub ExportToTextFile()

Dim WholeLine As String
Dim FNum As Integer
Dim sav_dir As String
Dim file_dir As String
Dim fsobj, fsfolder
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
sav_dir = file_path

Set fsobj = CreateObject("Scripting.FileSystemObject")
If Not fsobj.FolderExists(sav_dir) Then
       fsobj.CreateFolder sav_dir
End If
Sheets("Map_TXT").Select

Rows("217:1320").Select
Application.CutCopyMode = False
Selection.Delete

FNum = FreeFile()
    StartRow = .Cells(1).Row
    StartCol = .Cells(1).Column
    EndRow = 216
    EndCol = .Cells(.Cells.count).Column
End With

Open file_dir For Append Access Write As #FNum
For RowNdx = StartRow To EndRow
    WholeLine = ""
    For ColNdx = StartCol To EndCol

        If Cells(RowNdx, ColNdx).Value = "" Then
            CellValue = Chr(0) 'Chr(34) & Chr(34)
        Else
            CellValue = Cells(RowNdx, ColNdx).Value
        End If
        WholeLine = WholeLine & CellValue
    Next ColNdx
    WholeLine = Left(WholeLine, Len(WholeLine))
    Print #FNum, WholeLine
Next RowNdx
Close #FNum
EndMacro:
On Error 
End Sub

所以无论我做什么,这个函数都会在输出文件的末尾添加一个空行。这个空行是行号217,我必须摆脱这最后一行。 有人可以为我提供如何删除该文件中最后一行的解决方案吗? 提前谢谢

1 个答案:

答案 0 :(得分:2)

用于写入最后一行(当RowNdx = EndRow时)使用

Print #FNum, WholeLine;  '<<< note the semicolon