|
Option Explicit
Sub test1()
Dim strFileName$, strPath$, strSaveName$
Dim strResult$(), ar, br, y&, x&, n As Byte, strTxt$
DoApp False
strPath = ThisWorkbook.Path & "\"
strFileName = Dir(strPath & "*.txt")
Do Until strFileName = ""
strSaveName = strPath & Left(strFileName, InStrRev(strFileName, ".") - 1) & ".xlsm"
strTxt = ReadFromTextFile(ThisWorkbook.Path & "\" & strFileName)
ar = Split(strTxt, vbLf)
ReDim strResult(UBound(ar), UBound(Split(ar(0), vbTab)))
For y = 0 To UBound(ar)
br = Split(ar(y), vbTab)
For x = 0 To UBound(br)
strResult(y, x) = br(x)
Next x
Next y
With Workbooks.Open(strSaveName)
.ActiveSheet.Cells.Clear
.ActiveSheet.[A1].Resize(UBound(strResult) + 1, UBound(strResult, 2)) = strResult
.Close True
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
|
评分
-
3
查看全部评分
-
|