1
我一直在嘗試將多個大文本文件導入到同一工作簿中的多個電子表格中。感謝這個網站上的其他帖子,我已經拼湊出了一些似乎可以完成這項工作的VBA代碼。唯一的問題是,由於數據在文本文件中未標記,因此很難區分它們以進行分析。因此,我想在導入過程中爲每個電子表格添加相應的文本文件。我正在使用的代碼如下。導入多個文本文件時的標籤工作表Excel VBA
謝謝!
Sub ImportManyTXTs()
Dim strFile As String
Dim ws As Worksheet
strFile = Dir("I:\test\*.txt")
Do While strFile <> vbNullString
Set ws = Sheets.Add
With ws.QueryTables.Add(Connection:= _
"TEXT;" & "I:\test\" & strFile, Destination:=Range("$A$1"))
.Name = strFile
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileFixedColumnWidths = Array(7, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
strFile = Dir
Loop
End Sub
編輯:管理弄清楚。剛結束後添加了ws.Name = strFile。似乎工作得很好。
確保您遵循有效工作表名稱的規則:[您的圖表名稱正在殺死我的公式](http://blog.contextures.com/archives/2009/10/26/your-sheet-names-are-殺我的公式/) –
哈哈。是啊。這不是問題。管理弄清楚。剛結束後添加了ws.Name = strFile。似乎工作得很好。 –
我建議您輸入您的答案作爲問題的答案,並將其標記爲使用複選標記回答。是的,你可以提供你自己的答案。 –