PDA

View Full Version : Import txt files into excel



tommy1234
04-01-2010, 01:39 AM
Hello
I work with an ERP systems which can export .txt files.
I can export them into specific folder.
I want to import them into excel 2007 (without text to columns or any other change, just as it exported from the ERP) and insert each of them into different worksheet.
I have the code for browsing folders. so i need only to import the txt.

thanks

SamT
04-01-2010, 07:17 PM
(without text to columns or any other change, just as it exported from the ERP)

Do you want to import the contents of one .txt file into one cell of a sheet?

tommy1234
04-03-2010, 02:51 AM
Hi
No i want to import several .txt files. each .txt should be import into different worksheet.

mdmackillop
04-03-2010, 05:56 AM
Option Explicit
Sub Macro1()
Dim MyPath As String
Dim MyFile As String
Dim sh As Worksheet

MyPath = "C:\Stuff\" '<===== Change to suit
MyFile = Dir(MyPath & "*.txt")

Do Until MyFile = ""
Set sh = Sheets.Add
With sh.QueryTables.Add(Connection:="TEXT;" & MyPath & MyFile, _
Destination:=Range("$A$1"))
.Name = Split(MyFile, ".")(0)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
sh.Name = Split(MyFile, ".")(0)
MyFile = Dir
Loop
End Sub