|
本帖最后由 fzxba 于 2022-12-16 18:23 编辑
- Sub test2()
- Dim Conn As Object, Fso As Object, oFile As Object
- Dim ar() As String, br, cr, strFilename As String
- Dim i As Long, j As Long, k As Long, n As Byte
- Application.ScreenUpdating = False
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0 XML;Data Source=" & ThisWorkbook.FullName
- Cells.Clear
- For Each oFile In Fso.GetFolder(ThisWorkbook.Path).Files
- If InStr(LCase(oFile.Name), ".txt") > 0 Then
- n = FreeFile
- Open oFile For Input As #n
- br = Split(StrConv(InputB(LOF(n), #n), vbUnicode), vbCrLf)
- Close #n
- k = 0
- ReDim ar(UBound(br), UBound(Split(br(0), vbTab)))
- For i = LBound(br) To UBound(br)
- If Len(Replace(br(i), Chr(9), vbNullString)) Then
- cr = Split(br(i), vbTab)
- For j = LBound(cr) To UBound(cr)
- ar(k, j) = cr(j)
- Next
- k = k + 1
- End If
- Next
- Range("A1").Resize(k, UBound(ar, 2) + 1) = ar
- br = Split(oFile.Path, ".")
- br(UBound(br)) = "xlsx"
- strFilename = Join(br, ".")
- If Fso.FileExists(strFilename) Then Fso.DeleteFile strFilename
- Conn.Execute "SELECT * INTO [" & strFilename & "].[Sheet1] FROM [" & ActiveSheet.Name & "$]"
- Cells.Clear
- End If
- Next
- Conn.Close
- Set Conn = Nothing
- Set Fso = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|