返回列表 上一主題 發帖

Excel 輸出 txt,多餘空白行

Excel 輸出 txt,多餘空白行

大家好
目前要將工作部的欄位A的數據,輸出到文字檔txt中,再給其他程式執行使用
程式碼主要如下,但會有個問題就是輸出完成後的txt檔案中
實際上末端會有一個空白行
這導致其他程式執行時會出錯
想請問是否有辦法讓excel輸出時,可以避免有這空白行
謝謝
  1. torget = folderPath & "javainput.txt"
  2. Open torget For Output As #1

  3.     For i = 1 To filledCellsCount

  4.         outputinfo = Trim(ActiveSheet.Cells(i, 1).Value)
  5.         Print #1, outputinfo
  6.         
  7.         
  8.     Next i

  9. Close #1
複製代碼

回復 1# yenwang


    Sub test()


Dim fso As Object, filledCellsCount As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Set target = fso.CreateTextFile("d:\javainput.txt", True)

filledCellsCount = 8

For i = 1 To filledCellsCount - 1
   target.WriteLine Cells(i, 1)
Next
target.Write Cells(filledCellsCount, 1)
target.Close
Set fso = Nothing

End Sub

TOP

回復 2# quickfixer


萬分感謝
成功了:D

TOP

本帖最後由 Andy2483 於 2024-1-11 08:41 編輯

回復 2# quickfixer


    謝謝 yenwang前輩發表此主題,謝謝 quickfixer前輩指導
後學藉此帖學習到很多知識,學習心得如下:
Excel工作表資料:


執行後產生的 TEXT文字檔:


Sub TEST()
Dim fso, T$, target
Set fso = CreateObject("Scripting.FileSystemObject")
Set target = fso.CreateTextFile("d:\javainput.txt", True)
For i = 1 To [A65536].End(3).Row:   T = T & vbCrLf & Cells(i, 1):   Next
target.Write Mid(T, 3)
target.Close
Set fso = Nothing
End Sub
https://learn.microsoft.com/zh-t ... eatetextfile-method
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

        靜思自在 : 一個人的快樂.不是因為他擁有得多,而是因為他計較得少。
返回列表 上一主題