Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
DnD DnD is offline
external usenet poster
 
Posts: 3
Default automating import of several text files from specified folder

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).
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default automating import of several text files from specified folder

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).

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default automating import of several text files from specified folder

See also this page
http://www.rondebruin.nl/csv.htm



--

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


"DnD" wrote in message ...
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
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
Import data from multiple excel files in one folder WingZero Excel Programming 7 December 13th 07 07:30 PM
How to Import the path of files (100) in single folder to excel Techno Excel Programming 5 January 6th 07 10:12 PM
Open many *.tsv files in folder and import the data into Excel SunRace Excel Programming 1 March 16th 06 10:40 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven[_2_] Excel Programming 1 January 24th 06 04:23 AM


All times are GMT +1. The time now is 06:33 AM.

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"