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

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

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 &#39; Switch to the specific sheet if necessary

    &#39; Gets the path to the Excel file
    filePath = ThisWorkbook.Path &amp; &quot;\RxH.txt&quot;

    With ws
        lastRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row

        &#39; Iterate through each row and build the TXT data
        
        For i = 2 To lastRow &#39; Start from row 2 to skip the row of headers
            Dim invoiceNo As String
            invoiceNo = .Cells(i, &quot;C&quot;).Value
            Dim dashIndex As Long
            dashIndex = InStr(1, invoiceNo, &quot;-&quot;)

            txtData = txtData &amp; _
                Replace(.Cells(i, &quot;A&quot;).Value, &quot;,&quot;, &quot;&quot;) &amp; &quot;|&quot; &amp; _
                &quot;R1&quot; &amp; &quot;|&quot; &amp; _
                Left(invoiceNo, dashIndex - 1) &amp; &quot;|&quot; &amp; _
                Mid(invoiceNo, dashIndex + 1) &amp; &quot;|&quot; &amp; _
                Format(.Cells(i, &quot;B&quot;).Value, &quot;dd/mm/yyyy&quot;) &amp; &quot;|&quot; &amp; _
                Replace(.Cells(i, &quot;D&quot;).Value, &quot;,&quot;, &quot;&quot;)

            &#39; Adds a new line only if it is not the last row
            
            If i &lt; lastRow Then
                txtData = txtData &amp; vbCrLf &#39; vbCrLf add a new line
            End If
        Next i
    End With

    &#39; Writes TXT data to a file
    Open filePath For Output As #1
    Print #1, txtData &#39; Excludes the last extra line
    Close #1

    MsgBox &quot;TXT file generated successfully!&quot;, vbInformation
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:

确定