批处理读入TXT到EXCEL中

来源:百度知道 编辑:UC知道 时间:2024/06/14 15:00:10
我有很多txt文件,文件名按照时间排列的,文件内容是数据格式,tab分隔,我想把他们读进同一个EXCEL,请问如何处理呢,谢谢:)

Sub loadfile()
Dim directory As String
Dim file As String
Dim i As Integer

directory = "d:\txt文件夹\"
file = Dir(directory, 7)
i = 0

Do While file <> ""
i = i + 1
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = file

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & directory & file, _
Destination:=Range("A1"))
.Name = "04"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0