代码如下
Sub Txt文件处理()
Dim t, pT As String, fn As String, fnpt As String, mystr As String, k As Long
t = Timer
Range("A:B") = ""
Range("A:B").NumberFormatLocal = "@"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "没有选择文件夹"
Exit Sub
Else
pT = .SelectedItems(1)
End If
End With
fn = Dir(pT & "\*.txt")
Do While fn <> ""
fnpt = pT & "\" & fn
Open fnpt For Input As #1
Do While Not EOF(1)
Line Input #1, mystr
If InStr(mystr, "-------------") > 0 Then
mystr = Mid(mystr, 12, 6)
k = k + 1
Cells(k, "A") = mystr
Cells(k, "B") = Format(Left(fn, Len(fn) - 4), "000")
End If
Loop
Close #1
fn = Dir
Loop
MsgBox "处理完成,共用时" & Timer - t & "秒"
End Sub