|
Option Explicit
Sub test()
Dim strFileName$, strPath$, strResult$(), ar, br, y&, x&, n&, r&, c&, strTxt$
DoApp False
strPath = ThisWorkbook.Path & "\"
strFileName = Dir(strPath & "*.txt")
Do Until strFileName = ""
strTxt = ReadFromTextFile(ThisWorkbook.Path & "\" & strFileName)
ar = Split(strTxt, vbCrLf): r = 0: c = 0
ReDim strResult(1 To UBound(ar) + 1, 1 To 10 ^ 3)
For y = 0 To UBound(ar)
If Len(ar(y)) Then
br = Split(Replace(ar(y), ":", vbTab), vbTab)
r = r + 1
For x = 0 To UBound(br)
strResult(r, x + 1) = br(x)
Next x
If UBound(br) + 1 > c Then c = UBound(br) + 1
End If
Next y
With Workbooks.Add
With .Sheets(1).[A1].Resize(r, c)
.Value = strResult
.HorizontalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With
.SaveAs strPath & Left(strFileName, InStrRev(strFileName, ".") - 1)
.Close
End With
strFileName = Dir
Loop
DoApp
Beep
End Sub
Function DoApp(Optional b As Boolean = True)
With Application
.ScreenUpdating = b
.DisplayAlerts = b
.Calculation = -b * 30 - 4135
End With
End Function
Function ReadFromTextFile$(ByVal strFullName$, _
Optional ByVal strCharSet$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.Charset = strCharSet
.Open
.LoadFromFile strFullName
ReadFromTextFile = .ReadText
.Close
End With
End Function
|
|