下面的代码是:
Sub GenerateTxtFile()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim filePath As String
Dim txtData As String
Set ws = ActiveSheet ' Change to the specific worksheet if needed
' Get the path of the Excel file
filePath = ThisWorkbook.Path & "\RxH.txt"
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Loop through each row and construct the TXT data
For i = 2 To lastRow ' Start from row 2 to skip the headings row
Dim invoiceNo As String
invoiceNo = .Cells(i, "C").Value
Dim dashIndex As Long
dashIndex = InStr(1, invoiceNo, "-")
txtData = txtData & _
Replace(.Cells(i, "A").Value, ",", "") & "|" & _
"R1" & "|" & _
Left(invoiceNo, dashIndex - 1) & "|" & _
Mid(invoiceNo, dashIndex + 1) & "|" & _
Format(.Cells(i, "B").Value, "dd/mm/yyyy") & "|" & _
Replace(.Cells(i, "D").Value, ",", "") & vbCrLf ' vbCrLf adds a new line
Next i
End With
' Write the TXT data to a file
Open filePath For Output As #1
Print #1, Mid(txtData, 1, Len(txtData) - 2) ' Exclude the last vbCrLf
Close #1
MsgBox "TXT file generated successfully!", vbInformation
End Sub
问题是它生成的.txt文件底部多了一行,而我上传txt文件的系统无法处理它。有没有一种方法可以生成txt文件而不需要额外的一行?谢谢!
我期待的.txt文件生成没有一个额外的行在结束。
1条答案
按热度按时间4sup72z81#
尝试此更新的代码,其中添加了一个检查,以在添加vbCrLf之前验证它是否是范围的最后一行。