Sub uuu()
Dim folder_path$, file_name$
'--------------------------------
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub
folder_path = .SelectedItems(1)
End With
folder_path = folder_path & IIf(Right(folder_path, 1) = Application.PathSeparator, "", Application.PathSeparator)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set sh = Sheets.Add
With sh
file_name = Dir(folder_path & "*.txt")
Do While file_name <> ""
a = GetDataFromText(folder_path & file_name, "utf-8", vbCrLf, " ")
.UsedRange.EntireRow.Delete
.Cells(1, 1).Resize(UBound(a) + 1, UBound(a, 2) + 1) = a
.Copy
ActiveWorkbook.SaveAs FileName:=folder_path & Replace(file_name, "txt", "xlsx"), _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
file_name = Dir
Loop
.Delete
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Beep
End Sub
Function GetDataFromText(file_path$, file_charset$, row_delimeter$, column_delimeter$) As Variant
Dim a()
Dim rw, cl
Dim i&, j&
Dim txt$
'---------------------
With CreateObject("ADODB.Stream")
.Charset = file_charset
.Open
.LoadFromFile file_path
txt = .ReadText
.Close
End With
rw = Split(txt, row_delimeter)
cl = Split(rw(0), column_delimeter)
ReDim a(0 To UBound(rw), 0 To UBound(cl))
For i = 0 To UBound(a)
cl = Split(rw(i), column_delimeter)
For j = 0 To UBound(a, 2)
a(i, j) = cl(j)
Next
Next
GetDataFromText = a
End Function
Bookmarks