2016-11-30 84 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