在一個文件夾中有數百個.txt文件,想將其另存為.xls文件,保存時文件名與原文件名相同;且將內容按空格分列(.txt文件中格式相同),使用VBA可以實現該功能,代碼如下:
Dim Fso, Fl
Set Fso = CreateObject("Scripting.FileSystemObject")
For Each Fl In Fso.getfolder(ThisWorkbook.Path & "").Files
If Fl.Name <> ThisWorkbook.Name Then
Workbooks.OpenText (Fl)
With ActiveWorkbook
.SaveAs ThisWorkbook.Path & "" & Fl.Name & ".xls"
.Close 0
End With
End If
Next