[ASK] 1 macro to Convert txt into excel in mass

esteem

New Member
Joined
Dec 20, 2015
Messages
1
Dear gurus,

I need to convert txt into excel, I have large amount of txt file that need to convert into excel which has delimiter (eg, comma, tab, or pipeline ("|").
My requirement is to have txt file on one folder and the converted file in another folder.

My idea is:
1. Declare the folder name for the txt(this one should be variable), e.g. in cell A1 "c:\test\txt\"

2. set the folder name for the xlsx (this one should be variable), eg. in cell A2 "c:\test\excel\"

3. This one is nice to have, if we could Define the delimiter (this one should be variable also), eg. in cell A3 "|", or "," or "tab"

4. Last is Play button, to run the macro.

Maybe anyone can help me to build the macro for this ?

Thank you
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Esteem
To help you create a macro you could start with some thing like this
Code:
Sub LoopFoldersAndConvert() 'reference Microsoft Scripting Runtime under Tools > ReferencesDim fso As New FileSystemObject
Dim mFile As File
Dim mFolder As Folder
Dim folderPath, myfile, mysep, outf As String
Dim tempbook As Workbook

folderPath = ActiveWorkbook.Sheets(1).Range("A1").Value 'Your folder goes here
outf = ActiveWorkbook.Sheets(1).Range("A2").Value
mysep = ActiveWorkbook.Sheets(1).Range("A3").Value

Set fso = CreateObject("Scripting.FileSystemObject")
Set mFolder = fso.GetFolder(folderPath)
 
For Each mFile In mFolder.Files
    myfile = mFile.Path
    myoutf = Replace(myfile + ".xls", folderPath, outf)
    Set tempbook = Workbooks.OpenText(myfile, xlMSDOS, 1, xlDelimited, xlDoubleQuote, False, _
    False, False, False, False, True, mysep)
    rc = tempbook.SaveAs(myoutf, xlExcel8, "", "", False, False)
Next mFile
End Sub
Beware macro has a reference
Good luck
Cheers
Sergio
 
Upvote 0

Forum statistics

Threads
1,215,927
Messages
6,127,737
Members
449,401
Latest member
TTXS

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