2016-11-30 116 views
0

我有一个小但真正烦人的问题。我写了一个代码来分析一定数量的数据并以单元格格式显示结果。最后,结果应转换为文件格式。这是我输出到文件:Excel在输出文件末尾添加空行

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,我必须摆脱最后一行。 有人可以提供一个关于如何删除该文件中最后一行的解决方案吗? 预先感谢您

回答

2

写作的最后一行(当RowNdx = EndRow)使用

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

该死的,非常感谢你! – Schbabako