使用如下的VBA代码可实现批量自动导入数据。
程序会提供选定txt所在文件夹;
然后所有txt文件会导入到excel中;
按照先行后列的方式排列数据,即同一文件按行,不同文件按列排列。
Sub 宏1()
Dim myPath As String
Dim myFile As String
Dim i As Single
i = 1
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
.AllowMultiSelect = False
myPath = .SelectedItems(1)
End With
myFile = Dir(myPath & "\" & "*.txt")
Do While Len(myFile) <> 0
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & myPath & "\" & myFile, Destination:=Cells(1, i))
.Name = "1 - 副本"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
myFile = Dir
i = i + 1
Loop
End Sub
首先应该把 数据复制到excel 然后通过 公式来达到 但是你所说的条件不够全面 可以把文件发给我 我帮你弄