|
Sub test()
On Error Resume Next
Set fs = Application.FileSearch
With fs
.LookIn = ThisWorkbook.Path
.Filename = "*.txt"
If .Execute > 0 Then
If MsgBox("本文件夹下有" & .FoundFiles.Count & " 文件需要导入,确认导入吗?", vbYesNo, "导入确认") = vbNo Then Exit Sub
For i = 1 To .FoundFiles.Count
filenameStr = Mid(Left(.FoundFiles(i), Len(.FoundFiles(i)) - 4), Len(ThisWorkbook.Path) + 2)
Workbooks.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ThisWorkbook.Path & "\" & filenameStr & ".txt", Destination:= _
Range("A1"))
.Name = filenameStr
.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
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ChDir ThisWorkbook.Path
ActiveWorkbook.SaveAs Filename:= _
ThisWorkbook.Path & "\" & filenameStr & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWindow.Close
Next i
Else
MsgBox "没有需要导入的文件"
End If
End With
End Sub |
|