Repeat importing text

  • Thread starter Thread starter Huyeote
  • Start date Start date
H

Huyeote

Hi, all,

Posts following Clayton's question regarding how to import text in VBA are
helpful but the code can only import one text file each time. I have a
folder which contains a number of log files which have uniform format. The
number of files vary from time to time. I want to import all of them in XL.
Can anybody give me a sample code to determine the number of file that meets
its importing critera in a folder and load them into one sheet of XL? Or
give me hint where to go?

Regards,

Huyeote
 
Huyeote

The code below should give you a start on how to identify
the files which match your criteria.

Sub bbb()
ChDir "c:\temp"

With Application.FileSearch
.FileName = "*.txt"
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
End If
End With
End Sub

The line MsgBox .foundfiles(i) can be modified to input
the code you have about opening a text file. If the files
area all standard size, then you can select the entire
file, copy and then paste it to the output file. You can
then find the next blank cell in the output file
(something like range("a65536").end(xlup).offset
(1,0).select will take you there). You can then close the
opened text file, move to the next one and repeat.

HTH

Tony
 
Back
Top