|
Sub TEST()
Dim strFilename$, strPath$, wkb As Workbook
Dim vResult, arr, brr, strTxt$, i&, j&
Application.ScreenUpdating = False
Application.DisplayAlerts = False
strPath = ThisWorkbook.Path & "\"
strFilename = Dir(strPath & "*.txt")
Do Until strFilename = ""
strTxt = ReadFromText(strFilename)
arr = Split(strTxt, vbLf)
ReDim vResult(0 To UBound(arr), 0 To 6)
For i = 0 To UBound(arr)
If ReplaceEmptytxt(arr(i)) <> "" Then
brr = Split(arr(i), vbTab)
For j = 0 To UBound(brr)
vResult(i, j) = "'" & brr(j)
Next j
End If
Next i
Set wkb = Workbooks.Add
With wkb
.Sheets(1).[A1].Resize(i, j) = vResult
.SaveAs ThisWorkbook.Path & "\" & Left(strFilename, InStrRev(strFilename, ".") - 1)
With .Sheets(1).[A1].CurrentRegion
.HorizontalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
.Rows(1).Font.Bold = True
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With
.Sheets(1).Columns(5).Locked = False
.Sheets(1).Protect
.Close True
End With
strFilename = Dir
Loop
Set wkb = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Beep
End Sub
Function ReadFromText(ByVal FullName As String, Optional CharSet As Integer = -2) As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(FullName, 1, False, CharSet)
ReadFromText = f.Readall()
f.Close
Set f = Nothing
Set fs = Nothing
End Function
Function ReplaceEmptytxt(emptytxt)
Set reg = CreateObject("VBScript.RegExp")
With reg
.Global = True
.IgnoreCase = True
.Pattern = "[\u0001\u0009\u000a\u000d\u001c-\u0020\u007f-\u00fe]"
ReplaceEmptytxt = .Replace(emptytxt, "")
End With
End Function
|
评分
-
1
查看全部评分
-
|