导入多个文本文件时标记工作表Excel VBA

我一直在试图将多个大文本文件导入同一工作簿中的多个电子表格。 感谢在这个网站上的其他post,我已经拼凑了一些似乎是做这项工作的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 

编辑:pipe理弄清楚。 刚结束后添加了ws.Name = strFile。 似乎工作得很好。

pipe理弄清楚。 刚结束后添加了ws.Name = strFile。 似乎工作得很好。