Problem in exporting into excel when Separator in txt files is "|"(vertical bar) instead of ","

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
85
Office Version
  1. 2010
Platform
  1. Windows
Hello
Can anybody tell me a VB script wherein I have to export multiple txt files where the separator is instead of "," is "|"(vertical bar).
I have seen multiple codes where the separator is assumed to be a "," but no VB script for "|" which is a separator.

I have uploaded a sample txt file how it looks and there are multiple such files.

Your help is greatly and earnestly appreciated.

Regards

Anu
 

Attachments

  • problem.png
    problem.png
    72.9 KB · Views: 28
@anuradhagrewal: Please only mark a solution only if there is an answer to your question, even you posted the solution yourself. Otherwise, the question looks to be answered to the future readers, but no answer actually.

Another note: I can see that you have other questions that you confirmed that you received a working answer for each, but no marked solution. That would be also great if you could mark the solution post in those questions to help future readers. You can get the list of your questions by using the following link easily:

Regarding your question which requires Excel 2007 and VBA (I also suggest you edit your Office version information in your account if you are working in 2007, so we won't suggest technologies which are not existing in the corresponding Excel version.

About the VBA part: I recorded a macro in Excel 2007, and this is the result:
(You need to change the text file path)

Notice the delimiter parameter: TextFileOtherDelimiter = "|"

VBA Code:
 With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\smozgur\Desktop\data.txt", Destination:=Range("A1"))
        .Name = "data"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "|"
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
End With

Hope this helps.
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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