# Open multiple text files

Posted by martin Brennan on February 14, 2001 6:10 AM

I need to import 96 text files daily, take certain information from these and import the data to Excel for analysis.

At present, I am using:

Workbooks.OpenText Filename:="C:\P1\120101\1.TXT", _

DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _

Array(15, 1), Array(23, 1), Array(29, 1))

[a1:d8].Copy

or its equivalent, i.e., 2.TXT, 3.TXT, etc. 96 times.

This works, but surely there is a better way. Any bright ideas anyone? Can someone please point me to the right page in the manual?

Thanks

Martin

Posted by keekee on February 14, 2001 12:17 PM

this will open 3 text files and put them before the first sheet in your workbook.

Sub get_txt_files()

Dim I 'counter

Dim file_name, file_name2, file_name3 As String

For I = 1 To 3 'change to how many files you need

file_name = "c:\data\" & I ' directory of file

file_name2 = file_name & ".txt" ' adding extension

file_name3 = "all_good_macros.xls" ' file importing to

Workbooks.OpenText filename:=file_name2, Origin:=xlWindows, StartRow _

:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2), Array(9, 1), Array( _

12, 1), Array(31, 2), Array(37, 1), Array(39, 1), Array(41, 2), Array(57, 1))

Sheets(1).Select

Sheets(1).Move Before:=Workbooks(file_name3).Sheets(1)

Next I

End Sub

Hope this helps

Posted by Edward Clutter on February 15, 2001 7:58 AM

Hi Everyone,

I have a similar problem to this person. However instead of reading in 96 small files, I need to be able to read in 1 to 5 very large data files that are stored as *.txt files. I used the record macro feature to make the following macro. I basically used the open feature and deactivated all of the columns except the two I wanted. It works when I do it manually, but when I try to play back the macro, I get an "out of memory message". Anyone know why that is, and does anyone know of a better way to do what I want?

Thanks

Edward

Macro: (see listing below)

Sub read_in_data_file()

'

' read_in_data_file Macro

' Macro recorded 1/26/01 by clutter

'

'

Sheets("Sheet2").Select

ChDir "D:\Edward\Mercury_data_processing_010801\data\PPBreadboard\000911"

Workbooks.OpenText FileName:= _

"D:\Edward\Mercury_data_processing_010801\data\PPBreadboard\000911\MercuryBreadBBoard_20000909_1349.LOG" _

, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier _

:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:= _

False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array _

(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8 _

, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), _

Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array( _

21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), _

Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array(33, 1), Array( _

34, 1), Array(35, 1), Array(36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1), _

Array(41, 1), Array(42, 1), Array(43, 1), Array(44, 1), Array(45, 1), Array(46, 1), Array( _

47, 1), Array(48, 1), Array(49, 1), Array(50, 1), Array(51, 1), Array(52, 1), Array(53, 1), _

Array(54, 1), Array(55, 1), Array(56, 1), Array(57, 1), Array(58, 1), Array(59, 1), Array( _

60, 1), Array(61, 1), Array(62, 1), Array(63, 1), Array(64, 1), Array(65, 1), Array(66, 1), _

Array(67, 1), Array(68, 1), Array(69, 1), Array(70, 1), Array(71, 1), Array(72, 1), Array( _

73, 1), Array(74, 1), Array(75, 1), Array(76, 1), Array(77, 1), Array(78, 1), Array(79, 1), _

Array(80, 1), Array(81, 1), Array(82, 1), Array(83, 1), Array(84, 1), Array(85, 1), Array( _

86, 1), Array(87, 1), Array(88, 1), Array(89, 1), Array(90, 1), Array(91, 1), Array(92, 1), _

Array(93, 1), Array(94, 1), Array(95, 1), Array(96, 1), Array(97, 1), Array(98, 1), Array( _

99, 1), Array(100, 1))

ActiveWindow.ScrollWorkbookTabs Position:=xlFirst

ActiveWindow.ScrollWorkbookTabs Position:=xlLast

Columns("A:A").EntireColumn.AutoFit

ActiveWindow.ScrollWorkbookTabs Position:=xlFirst

ActiveWindow.ScrollWorkbookTabs Position:=xlFirst

ActiveWindow.SmallScroll Down:=33

ActiveWindow.ScrollRow = 17028

ActiveWindow.ScrollRow = 26783

ActiveWindow.ScrollRow = 29207

ActiveWorkbook.SaveAs FileName:= _

"D:\Edward\Mercury_data_processing_010801\data\PPBreadboard\000911\data.xls", _

FileFormat:=xlNormal, Password:="", WriteResPassword:="", _

ReadOnlyRecommended:=False, CreateBackup:=False

Range("A29210").Select

Workbooks.Open FileName:= _

"D:\Edward\Mercury_data_processing_010801\data\testcase.xls"

Range("B15").Select

ActiveWorkbook.Close

ActiveWorkbook.Close

Sheets("Sheet2").Select

Sheets("Sheet2").Name = "Data"

Range("A1").Select

Workbooks.Open FileName:= _

"D:\Edward\Mercury_data_processing_010801\data\PPBreadboard\000911\data.xls"

ActiveWindow.Visible = False

ActiveSheet.OLEObjects.Add(FileName:= _

"D:\Edward\Mercury_data_processing_010801\data\PPBreadboard\000911\data.xls", _

Link:=False, DisplayAsIcon:=False).Select

Sheets("AutoPlot - GUI").Select

ActiveWorkbook.Save

Sheets("Data").Select

Windows("Worksheet in autoplot_devl_012601").Visible = True

Selection.Verb Verb:=xlPrimary

ActiveWindow.WindowState = xlMaximized

Range("B29214").Select

ActiveWorkbook.Close

Sheets("AutoPlot - GUI").Select

ActiveWorkbook.Save

End Sub

Posted by Martin Brennan on February 15, 2001 3:35 PM

Keekee,

Thanks a lot, works like a dream!

Martin