VBA代码,防止生成.txt文件时添加额外的行。

huangapple go评论92阅读模式
英文:

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:

  1. Sub GenerateTxtFile()
  2. Dim ws As Worksheet
  3. Dim lastRow As Long
  4. Dim i As Long
  5. Dim filePath As String
  6. Dim txtData As String
  7. Set ws = ActiveSheet ' Change to the specific worksheet if needed
  8. ' Get the path of the Excel file
  9. filePath = ThisWorkbook.Path & "\RxH.txt"
  10. With ws
  11. lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  12. ' Loop through each row and construct the TXT data
  13. For i = 2 To lastRow ' Start from row 2 to skip the headings row
  14. Dim invoiceNo As String
  15. invoiceNo = .Cells(i, "C").Value
  16. Dim dashIndex As Long
  17. dashIndex = InStr(1, invoiceNo, "-")
  18. txtData = txtData & _
  19. Replace(.Cells(i, "A").Value, ",", "") & "|" & _
  20. "R1" & "|" & _
  21. Left(invoiceNo, dashIndex - 1) & "|" & _
  22. Mid(invoiceNo, dashIndex + 1) & "|" & _
  23. Format(.Cells(i, "B").Value, "dd/mm/yyyy") & "|" & _
  24. Replace(.Cells(i, "D").Value, ",", "") & vbCrLf ' vbCrLf adds a new line
  25. Next i
  26. End With
  27. ' Write the TXT data to a file
  28. Open filePath For Output As #1
  29. Print #1, Mid(txtData, 1, Len(txtData) - 2) ' Exclude the last vbCrLf
  30. Close #1
  31. MsgBox "TXT file generated successfully!", vbInformation
  32. End Sub

If you have any more questions or need further assistance, please feel free to ask.

英文:

Hello i have the following code:

  1. Sub GenerateTxtFile()
  2. Dim ws As Worksheet
  3. Dim lastRow As Long
  4. Dim i As Long
  5. Dim filePath As String
  6. Dim txtData As String
  7. Set ws = ActiveSheet ' Change to the specific worksheet if needed
  8. ' Get the path of the Excel file
  9. filePath = ThisWorkbook.Path & "\RxH.txt"
  10. With ws
  11. lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  12. ' Loop through each row and construct the TXT data
  13. For i = 2 To lastRow ' Start from row 2 to skip the headings row
  14. Dim invoiceNo As String
  15. invoiceNo = .Cells(i, "C").Value
  16. Dim dashIndex As Long
  17. dashIndex = InStr(1, invoiceNo, "-")
  18. txtData = txtData & _
  19. Replace(.Cells(i, "A").Value, ",", "") & "|" & _
  20. "R1" & "|" & _
  21. Left(invoiceNo, dashIndex - 1) & "|" & _
  22. Mid(invoiceNo, dashIndex + 1) & "|" & _
  23. Format(.Cells(i, "B").Value, "dd/mm/yyyy") & "|" & _
  24. Replace(.Cells(i, "D").Value, ",", "") & vbCrLf ' vbCrLf adds a new line
  25. Next i
  26. End With
  27. ' Write the TXT data to a file
  28. Open filePath For Output As #1
  29. Print #1, Mid(txtData, 1, Len(txtData) - 2) ' Exclude the last vbCrLf
  30. Close #1
  31. MsgBox "TXT file generated successfully!", vbInformation
  32. 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。

  1. Sub GenerateTxtFile()
  2. Dim ws As Worksheet
  3. Dim lastRow As Long
  4. Dim i As Long
  5. Dim filePath As String
  6. Dim txtData As String
  7. Set ws = ActiveSheet ' 切换到特定工作表(如果需要的话)
  8. ' 获取Excel文件的路径
  9. filePath = ThisWorkbook.Path & "\RxH.txt"
  10. With ws
  11. lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  12. ' 遍历每一行并构建TXT数据
  13. For i = 2 To lastRow ' 从第2行开始以跳过标题行
  14. Dim invoiceNo As String
  15. invoiceNo = .Cells(i, "C").Value
  16. Dim dashIndex As Long
  17. dashIndex = InStr(1, invoiceNo, "-")
  18. txtData = txtData & _
  19. Replace(.Cells(i, "A").Value, ",", "") & "|" & _
  20. "R1" & "|" & _
  21. Left(invoiceNo, dashIndex - 1) & "|" & _
  22. Mid(invoiceNo, dashIndex + 1) & "|" & _
  23. Format(.Cells(i, "B").Value, "dd/mm/yyyy") & "|" & _
  24. Replace(.Cells(i, "D").Value, ",", "")
  25. ' 仅在不是最后一行时添加新行
  26. If i < lastRow Then
  27. txtData = txtData & vbCrLf ' vbCrLf 添加新行
  28. End If
  29. Next i
  30. End With
  31. ' 将TXT数据写入文件
  32. Open filePath For Output As #1
  33. Print #1, txtData ' 排除最后的额外行
  34. Close #1
  35. MsgBox "TXT文件生成成功!", vbInformation
  36. 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.

  1. Sub GenerateTxtFile()
  2. Dim ws As Worksheet
  3. Dim lastRow As Long
  4. Dim i As Long
  5. Dim filePath As String
  6. Dim txtData As String
  7. Set ws = ActiveSheet &#39; Switch to the specific sheet if necessary
  8. &#39; Gets the path to the Excel file
  9. filePath = ThisWorkbook.Path &amp; &quot;\RxH.txt&quot;
  10. With ws
  11. lastRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row
  12. &#39; Iterate through each row and build the TXT data
  13. For i = 2 To lastRow &#39; Start from row 2 to skip the row of headers
  14. Dim invoiceNo As String
  15. invoiceNo = .Cells(i, &quot;C&quot;).Value
  16. Dim dashIndex As Long
  17. dashIndex = InStr(1, invoiceNo, &quot;-&quot;)
  18. txtData = txtData &amp; _
  19. Replace(.Cells(i, &quot;A&quot;).Value, &quot;,&quot;, &quot;&quot;) &amp; &quot;|&quot; &amp; _
  20. &quot;R1&quot; &amp; &quot;|&quot; &amp; _
  21. Left(invoiceNo, dashIndex - 1) &amp; &quot;|&quot; &amp; _
  22. Mid(invoiceNo, dashIndex + 1) &amp; &quot;|&quot; &amp; _
  23. Format(.Cells(i, &quot;B&quot;).Value, &quot;dd/mm/yyyy&quot;) &amp; &quot;|&quot; &amp; _
  24. Replace(.Cells(i, &quot;D&quot;).Value, &quot;,&quot;, &quot;&quot;)
  25. &#39; Adds a new line only if it is not the last row
  26. If i &lt; lastRow Then
  27. txtData = txtData &amp; vbCrLf &#39; vbCrLf add a new line
  28. End If
  29. Next i
  30. End With
  31. &#39; Writes TXT data to a file
  32. Open filePath For Output As #1
  33. Print #1, txtData &#39; Excludes the last extra line
  34. Close #1
  35. MsgBox &quot;TXT file generated successfully!&quot;, vbInformation
  36. End Sub

huangapple
  • 本文由 发表于 2023年5月11日 03:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定