0
在Excel 2010中,我想將文件夾中的所有文本文件數據加載到一個Worksheet中。將所有文本文件加載到一個工作表?
每張紙應該有一個文本文件的數據。
每張紙都是用第一行上的標題分隔的管道。
更新宏
Sub LoadTextFilesLoop()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object associated with the directory
Set objFolder = objFSO.GetFolder("C:\Export\")
'Loop through the Files collection
For Each objFile In objFolder.Files
NewFileImport (objFile.Name)
Next
'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
End Sub
Sub NewFileImport(FileName)
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Export\FileName", _
Destination:=Range("$A$1"))
.Name = FileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
循環不加載數據。
我仍然必須弄清楚如何獲得每個文件中的列數。
.TextFileColumnDataTypes = Array(1, 1, 1)
這行代表該文件中的三列。這應該根據文件列而有所不同。