View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default HELP: Import several TXT files into Excel

You could use File|open to import the text file and then copy to its new
postion. The plop that code into a loop.

If all the text files were in one dedicated folder (so no extra files are
processed), it might even work ok:

Option Explicit
Sub testme01()

Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String
Dim DestCell As Range
Dim NewWks As Worksheet
Dim wks As Worksheet

'change to point at the folder to check
myPath = "c:\my documents\excel\textfiles"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFile = ""
On Error Resume Next
myFile = Dir(myPath & "*.txt")
On Error GoTo 0
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

Application.ScreenUpdating = False

'get the list of files
fCtr = 0
Do While myFile < ""
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
myFile = Dir()
Loop

If fCtr 0 Then

Set NewWks = Workbooks.Add(1).Worksheets(1)
Set DestCell = NewWks.Range("a1")

For fCtr = LBound(myNames) To UBound(myNames)

Application.StatusBar _
= "Processing: " & myNames(fCtr) & " at: " & Now

Workbooks.OpenText Filename:=myPath & myNames(fCtr), _
Origin:=437, StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, _
Space:=False, Other:=False, FieldInfo:=Array(1, 2)

Set wks = ActiveSheet
DestCell.Value = "'" & myNames(fCtr)
wks.Columns(1).Copy _
Destination:=DestCell.Offset(0, 1)
wks.Parent.Close savechanges:=False

Set DestCell = DestCell.Offset(0, 1)

Next fCtr
End If

With Application
.ScreenUpdating = True
.StatusBar = False
End With

End Sub


luis wrote:

Dear all,

I need to import 100 TXT files into a single excel file for my Ph. D.
Dissertation.
All the files have the same structu only 1 column with 60 rows each,
like this:

01 - I
02 - C
03 - C
60 - I

I'd need to end up with an excel file that included 250 columns, one
for each file, and the name of the corresponding file on top of each
column, like this:

File1 File2 File100
01 - I 01 - C 01 - C
02 - C 02 - I 02 - I
03 - C 03 - C 03 - I
60 - I 60 - C 60 - I

I have little Excel experience, but I know how to paste code into a
module in the Visual Basic Editor. Please find below the code I get if
I import 1 single file into my Excel Workbook, in case it helps.

YOUR HELP WILL BE GREATLY APPRECIATED!!!

THANK YOU!!!!!!!!!!!!!!!!!!!!! ;-D

Luis

*** CODE AFTER IMPORTING 1 TXT FILE ***

Sub importTextFile()
'
' importTextFile Macro
' Macro recorded 08/04/2006 by luis cerezo ceballos
'

'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\aaqpresults\cerezo-int2-dah38-exp-rel.txt",
Destination:=Range("A1") _
)
.Name = "cerezo-int2-dah38-exp-rel"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub


--

Dave Peterson