Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

importing *.txt and save each file to .xls file

Status
Not open for further replies.

ronabo

MIS
Jun 29, 2003
3
US
Need help in creating a macro that reads multiple pipe delimited .txt files and save them into an .xls files. Please help me for I have been trying to make this work for weeks now with no luck.
 
Hi ronabo,
I am not a developer so cant help much, but I do have a file with code to import all csv files from a folder. This code may help (Obviously you will need to change the file and dir paths appropriately).You may be able to adapt this to import pipe delimited txt files. Good Luck :)

Sub GetAllFiles()
Dim MyFile As String
'\* Returns first found file of specified type...
DirPath = ("C:\Software Licensing\software\")
MyFile = Dir("C:\Software Licensing\software\*.CSV", vbNormal)

'\* Pass filename to routine to import it...
ImportMyFiles MyFile
'\* Define loop...
Do Until MyFile = ""
'\* Returns next file of type above...
MyFile = Dir
'\* If no more files found, exit loop...
If MyFile = "" Then Exit Do
'\* If not this workbook (assuming this is saved
'\* in the same folder as the files to be imported),
'\* exit loop...
If Not MyFile = ThisWorkbook.Name Then _
ImportMyFiles MyFile
Loop
'\* Save the workbook
ThisWorkbook.Sheets("Sheet1").Select
Range("A1").Select
End Sub

'\* This process will open the file passed to it
'\* and copy the first sheet to the last sheet of
'\* the workbook the macro is run from.
'\* The file is then closed without saving changes.

Sub ImportMyFiles(FileToImport As String)
Dim TWB As Workbook, IWB As Workbook
Set TWB = ThisWorkbook
Set IWB = Workbooks.Open(Filename:=DirPath & FileToImport, UpdateLinks:=False)
IWB.Sheets(1).Copy After:=TWB.Sheets(TWB.Sheets.Count)
IWB.Close SaveChanges:=False
End Sub
 
You will need to add
ThisWorkbook.Save
where it says '\* Save the workbook

 
Thanks, Waynerenaud. That will get me started.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top