Pipe-delimited Text File Format

mamun_ges

Board Regular
Joined
Jul 21, 2016
Messages
58
Hi, I am using the below code to Generate a text File in pipe Delimited format.
VBA Code:
Sub ExportTxt()

    Dim UsedRows As Long
    Dim UsedColumns As Long
    Dim R As Long, C As Long
  
    '// Define a suitable file name
    Open "E:\Folder\" & "TD.txt" For Output As #1
    With ActiveSheet
        UsedRows = .UsedRange.Rows.Count
        UsedColumns = .UsedRange.Columns.Count
        For R = 2 To UsedRows
            For C = 1 To UsedColumns
                Select Case C
                    Case 2              ' Numeric Column with 2 decimals
                        Print #1, Format(.Cells(R, C), "#.00"); "|";
                  
                    Case UsedColumns    ' Last Column end of line
                        Print #1, .Cells(R, C)
                  
                    Case Else
                        Print #1, .Cells(R, C); "|";
                  
                End Select
            Next C
        Next R
    End With

    Close #1

End Sub

But now I have to have a Pipe after the last line.
Like below (As an example)
ASD|DEPFR|2451|ASDFERTT|

What to change in the code to achieve the expected result.
Any suggestion?

Thanks and Best regards.
 
Thanks All for the response.

Yes, The pipe worked at the last used column but it breaks the line. (See Test image) Text.jpg
The line should be maintained properly same as the excel file. (See the expected Result Image)Expected Result.jpg

Here I don't get any option to attach the file.
I am attaching a Sample File to download the file from my google drive. Hope It helps understand my worksheet.


Thanks and best regards
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Can you post the code you tried.
(The whole routine like in your original post)
 
Last edited:
Upvote 0
I specifically asked that you COPY in the code I changed since you said it did not work previously.
You have not done so. You have added a semi-colon to the end of the line of code I provided which is what is causing the text output be continuous without a line break.

How about you just replace your entire code with this:
VBA Code:
    Dim UsedRows As Long
    Dim UsedColumns As Long
    Dim R As Long, C As Long
 
    '// Define a suitable file name
    Open "E:\Upload Folder\" & "TD.txt" For Output As #1
    With ActiveSheet
        UsedRows = .UsedRange.Rows.Count
        UsedColumns = .UsedRange.Columns.Count
        For R = 2 To UsedRows
            For C = 1 To UsedColumns
                Select Case C
                    Case 2              ' Numeric Column with 2 decimals
                        Print #1, Format(.Cells(R, C), "#.00"); "|";
              
                    Case UsedColumns    ' Last Column end of line
                        Print #1, .Cells(R, C); "|"
              
                    Case Else
                        Print #1, .Cells(R, C); "|";
              
                End Select
            Next C
        Next R
    End With

    Close #1

End Sub

Result

1632433196300.png
 
Last edited:
Upvote 0
Solution
According to the post #11 an easy VBA demonstration for starters :​
VBA Code:
Sub Demo1()
  Const D = "|", T = "E:\Upload Folder\TD.txt"
    Dim F%, R&
        F = FreeFile
        Open T For Output As #F
    With [A1].CurrentRegion.Rows
        For R = 2 To .Count
            Print #F, Join(Application.Index(.Item(R).Value, 1, 0), D); D
        Next
    End With
        Close #F
        Shell "Notepad " & T, 1
End Sub
 
Upvote 0
The correction of the initial VBA procedure - revamped without the useless ! - in case the cells formatting must be exported too :​
VBA Code:
Sub ExportTxt()
    Dim F%, R As Long, C%
        F = FreeFile
        Open "E:\Folder\TD.txt" For Output As #F
    With [A1].CurrentRegion
        For R = 2 To .Rows.Count
            For C = 1 To .Columns.Count:  Print #F, .Cells(R, C).Text; "|";:  Next
            Print #F,
        Next
    End With
        Close #F
End Sub
 
Upvote 0
@mamun_ges - I was just replying to Marc and ended up revisited your old post.
You seem to have started this thread using code before changing Case 2 to Case 5.
I am pretty sure your numbers are in Column 5 so don't forget to change the Case 2 to be Case 5
 
Upvote 0
The correction of the initial VBA procedure - revamped without the useless ! - in case the cells formatting must be exported too :
Hello Marc, typically in interfaces you want to keep the formatting for date columns but ignore it for numbers.
General format for numbers sometimes works but often you want to show 2 decimal places.
 
Upvote 0
As it depends on the expected result … Post #16 code keeps the cell formatting without any useless Select Case block …​
As any good enough application does not need 2 decimal places to import data, in this case post #14 does the job.​
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top