PC Review


Reply
Thread Tools Rate Thread

automating import of several text files from specified folder

 
 
DnD
Guest
Posts: n/a
 
      29th Dec 2007
i need a macro that will automatically import text files (fixed width) from a
specific folder. the number and names of the files in the folder will vary at
any given time. i would also like the files to be imported to the same sheet
(there's not too much data per text file).
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      29th Dec 2007
See if this helps. You have to change ColTable for the number of columns you
have, the length and starting position.

Sub fixwidth()
Const ForReading = 1, ForWriting = 2, _
ForAppending = 3
Const Folder = "c:\temp\test\"
Const StartPos = 0
Const ColWidth = 1

Dim ColTable(6, 2)
ColTable(0, StartPos) = 1
ColTable(0, ColWidth) = 10
ColTable(1, StartPos) = 11
ColTable(1, ColWidth) = 5
ColTable(2, StartPos) = 16
ColTable(2, ColWidth) = 8
ColTable(3, StartPos) = 24
ColTable(3, ColWidth) = 3
ColTable(4, StartPos) = 27
ColTable(4, ColWidth) = 6
ColTable(5, StartPos) = 33
ColTable(5, ColWidth) = 4

NumberColumns = UBound(ColTable)

Set fs = CreateObject("Scripting.FileSystemObject")

If Range("A1") = "" Then
RowCount = 1
Else
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
RowCount = Lastrow
End If

First = True
Do
If First = True Then
Filename = Dir(Folder & "*.txt")
First = False
Else
Filename = Dir()
End If
If Filename <> "" Then
Set fin = fs.OpenTextFile(Folder & Filename, _
ForReading, TristateFalse)
Do While fin.AtEndOfStream <> True
readdata = fin.readline

For Colcount = 0 To (NumberColumns - 1)
Data = Mid(readdata, _
ColTable(Colcount, StartPos), _
ColTable(Colcount, ColWidth))

Cells(RowCount, Colcount + 1) = Data
Next Colcount
RowCount = RowCount + 1
Loop
fin.Close
End If
Loop While Filename <> ""
End Sub


"DnD" wrote:

> i need a macro that will automatically import text files (fixed width) from a
> specific folder. the number and names of the files in the folder will vary at
> any given time. i would also like the files to be imported to the same sheet
> (there's not too much data per text file).

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      29th Dec 2007
See also this page
http://www.rondebruin.nl/csv.htm



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"DnD" <(E-Mail Removed)> wrote in message news:A07C7BEA-9765-4AD6-B65E-(E-Mail Removed)...
>i need a macro that will automatically import text files (fixed width) from a
> specific folder. the number and names of the files in the folder will vary at
> any given time. i would also like the files to be imported to the same sheet
> (there's not too much data per text file).

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Multiple Text files in Folder =?Utf-8?B?S2lyayBQLg==?= Microsoft Access External Data 3 30th Aug 2007 08:28 PM
Automating import of dynamic text file into MS Access FuzzyLogic Microsoft Access 2 26th Jul 2006 09:04 PM
Automating import of XML files received in Outlook Mus' via AccessMonster.com Microsoft Access External Data 2 24th Nov 2005 03:34 PM
Automating the import of XML files =?Utf-8?B?VGltIEZldw==?= Microsoft Access External Data 0 10th Mar 2005 05:27 PM
Need a simple macro that will import any one of a number of text files in a folder Steve Microsoft Excel Discussion 3 15th Sep 2004 02:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:38 PM.