英文:
VBA Code to prevent adding an extra line when it generates a .txt file
问题
I have translated the code portion for you. Here it is:
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
If you have any more questions or need further assistance, please feel free to ask.
英文:
Hello i have the following code:
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
The problem is that its generating the .txt file with an extra line at the bottom and the system where I upload the txt file can't process it. Is there a way for the txt file to be generated without that extra line?
Thanks!
Im expecting for the .txt file to be generated without an extra line at the end.
答案1
得分: 1
尝试使用此更新后的代码,其中添加了检查以验证是否是范围的最后一行后再添加 vbCrLf。
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 ' 切换到特定工作表(如果需要的话)
' 获取Excel文件的路径
filePath = ThisWorkbook.Path & "\RxH.txt"
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' 遍历每一行并构建TXT数据
For i = 2 To lastRow ' 从第2行开始以跳过标题行
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, ",", "")
' 仅在不是最后一行时添加新行
If i < lastRow Then
txtData = txtData & vbCrLf ' vbCrLf 添加新行
End If
Next i
End With
' 将TXT数据写入文件
Open filePath For Output As #1
Print #1, txtData ' 排除最后的额外行
Close #1
MsgBox "TXT文件生成成功!", vbInformation
End Sub
希望这有帮助!如果有其他问题,请随时告诉我。
英文:
Try this updated code, where a check is added to verify if it is the last row of the range before adding vbCrLf.
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 ' Switch to the specific sheet if necessary
' Gets the path to the Excel file
filePath = ThisWorkbook.Path & "\RxH.txt"
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Iterate through each row and build the TXT data
For i = 2 To lastRow ' Start from row 2 to skip the row of headers
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, ",", "")
' Adds a new line only if it is not the last row
If i < lastRow Then
txtData = txtData & vbCrLf ' vbCrLf add a new line
End If
Next i
End With
' Writes TXT data to a file
Open filePath For Output As #1
Print #1, txtData ' Excludes the last extra line
Close #1
MsgBox "TXT file generated successfully!", vbInformation
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论