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:
This should be it then
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 = Split(CreateObject("scripting.filesystemobject").opentextfile(c00).readall, vbLf)
          With .Sheets(1).Cells(1, 1)
             .Resize(UBound(ar)) = ar
             .CurrentRegion.TextToColumns .Offset, xlDelimited, xlNone, True, , , , , True, "|"
          End With
         .SaveAs c01, 51
         .Close
       End With
    End If
 End With
End Sub
 
Upvote 0

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.
A small change, forgot to transpose the array

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 = Split(CreateObject("scripting.filesystemobject").opentextfile(c00).readall, vbLf)
          With .Sheets(1).Cells(1, 1)
             .Resize(UBound(ar) + 1) = Application.Transpose(ar)
             .CurrentRegion.TextToColumns .Offset, xlDelimited, xlNone, True, , , , , True, "|"
          End With
         .SaveAs c01, 51
         .Close
       End With
    End If
 End With
End Sub
 
Upvote 0
Solution
You're welcome. This shows how important the way of asking is ;) .
 
Upvote 0
Hi instead of creating a new excel file can I use the existing one please see my other thread here

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
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