Excel VBA to load the text file and convert to tab delimited file

ExcelDev

New Member
Joined
Sep 14, 2021
Messages
11
Office Version
  1. 365
I am having a text file where I would like to convert it to tab delimited file by trimming the extra spaces if any.

Sample format in my file is as follows

VBA Code:
0|04/07/1998|               |               |               |     |0|    |          | |N|

In an excel I would like to have a button where it should select the file to convert and once after selecting it should convert and save it to a new file.
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Do you want to overwrite the old textfile?

VBA Code:
Sub jec()
 With Application.FileDialog(msoFileDialogFilePicker)
    If .Show Then
       c00 = .SelectedItems(1)
       c01 = Replace(Split(c00, "\")(UBound(Split(c00, "\"))), ".txt", "") & "new" & ".txt"
       With CreateObject("scripting.filesystemobject")
          .createtextfile(c01).write Application.Trim(.opentextfile(c00).readall)
       End With
    End If
 End With
End Sub
 
Last edited:
Upvote 0
Do you want the text in 1 cell?
 
Upvote 0
In different different cell, what I am trying to do is I have an excel where a button is placed, when he click on it will prompt to select a text file. The selected text file should convert to a tab delimited file.
 
Upvote 0
To me the desired output is still not clear. You could give this a try

VBA Code:
Sub jec2()
 With Application.FileDialog(msoFileDialogFilePicker)
    If .Show Then
       c00 = .SelectedItems(1)
       c01 = Replace(Split(c00, "\")(UBound(Split(c00, "\"))), ".txt", "") & "new" & ".xlsx"
       With Workbooks.Add
          ar = Filter(Split(Replace(CreateObject("scripting.filesystemobject").opentextfile(c00).readall, " ", "@"), "|"), "@", False)
         .Sheets(1).Cells(1, 1).Resize(, UBound(ar)) = ar
         .SaveAs c01, 51
         .Close
       End With
    End If
 End With
End Sub
 
Upvote 0
Thanks if I have data in another line will it store in to a new row like

Rich (BB code):
0|04/07/1998|               |               |               |     |0|    |          | |N|
0|04/07/1998|               |               |               |     |0|    |          | |N|
 
Upvote 0
Also if possible keep the empty text as is and if there are any space after text I would like to trim. Like
Rich (BB code):
0|04/07/1998|               |               |               |     |0|    |          | |N         |
 
Upvote 0
What about the straight dashes
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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