Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Importing multiple text files into one work book

I'm trying to create VBA code that will import a varying number of
text files into Excel 2000. The files will not have a .txt extension
but rather a varying numeric extension (.1, .2, .3 and so forth.
Using the macro recorder I have set up several macros that import the
text (only one file) and format it the way I want then cut and past it
into a worksheet. What I want to know how to do is import every file
in a folder, one at a time, and add the text to different rows of the
same worksheet. I have very little programming experience. Any help
will be appreciated. :)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default Importing multiple text files into one work book

Here are some pieces of code that I quickly copied piecemeal from another
project I did a while back. This is NOT debugged code, but should give you
some idea to get going. Basically, add a reference to the Script library to
your project (pull down the Tools menu and choose the References command),
then use it to open each data file as a text stream, read each line into a
single cell on a blank worksheet, then move down one row. Parse all of the
data in column $A out to multiple columns and format as desired (code you
have already written).

'Reference the "Microsoft Scripting Runtime" in Tools|References.

Dim varTXTFileList As Variant 'Array of filenames to process.
Dim varFileName As Variant 'Individual file name.
Dim fs As Object 'File System object.
Dim f As Object 'File object.
Dim ts As Object 'Text Stream object.
Dim ws As Worksheet
Dim rngCell As Range 'Cell to put a single imported line into.

'Create a new, empty Excel workbook to import the data into.
Set wbTextFile = Workbooks.Add 'Create a new Excel workbook.
Set ws = wbTextFile.Worksheets(1) 'Set reference to the 1st worksheet.
Set rngCell = ws.Cells(1, 1) 'Set rngCell to cell $A$1.

'Get the list of filenames (multi-select).
varTXTFileList = Application _
.GetOpenFilename( _
FileFilter:="All Files (*.*),*.*", _
FilterIndex:=1, _
Title:="Open Data Files", _
MultiSelect:=True)

For Each varFileName In varTXTFileList

'Set references to the text file and open as a text stream.
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(Cstr(varFileName))
Set ts = f.OpenAsTextStream(1, 0) 'Open the text file for reading
'only in ASCII format.

'Import the data from the Text Stream to the Excel workbook,
'line-by-line.
Do While Not ts.AtEndOfStream 'Check end-of-file status (?).
rngCell.Value = ts.readline 'Read line of text; put in cell.
Set rngCell = rngCell.Offset(1, 0) 'Advance cell to the next row.
Loop

ts.Close 'Close text stream.
Next varFileName

'Now parse the data out into the columns using the TextToColumns method of
the Range object.
--
HTH,
Bill


"mike" wrote in message
om...
I'm trying to create VBA code that will import a varying number of
text files into Excel 2000. The files will not have a .txt extension
but rather a varying numeric extension (.1, .2, .3 and so forth.
Using the macro recorder I have set up several macros that import the
text (only one file) and format it the way I want then cut and past it
into a worksheet. What I want to know how to do is import every file
in a folder, one at a time, and add the text to different rows of the
same worksheet. I have very little programming experience. Any help
will be appreciated. :)



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collating multiple .csv files into a single work book Mr_Wilf[_2_] Excel Discussion (Misc queries) 9 November 9th 09 07:21 PM
importing multiple text files into the same worksheet Mike D Excel Discussion (Misc queries) 4 July 15th 05 10:39 AM
Importing multiple Text files into Excel 2003 JMA Excel Discussion (Misc queries) 5 May 5th 05 09:48 PM
importing multiple text files URGENT!!! HELP tasha Excel Worksheet Functions 0 December 19th 04 04:26 PM
importing multiple text files??? tashayu Excel Discussion (Misc queries) 0 December 19th 04 02:43 PM


All times are GMT +1. The time now is 03:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"