如何将多个txt批量导入Excel并以txt文件名命名工作表名称
2个回答
展开全部
使用如下VBA代码实现,复制粘贴到VBA模块中运行即可(alt+F11)。
Sub import_txt()
On Error Resume Next
Dim my_Link, my_Doc As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
.AllowMultiSelect = False
my_Link = .SelectedItems(1)
End With
Dim doc_Count As Single
doc_Count = 0
my_Doc = Dir(my_Link & "\" & "*.txt")
Do While Len(my_Doc) <> 0
Worksheets.Add
ActiveSheet.Name = my_Doc
Dim my_Word As String
Dim i As Single
i = 1
Open my_Link & "\" & my_Doc For Input As #1
Do While Not EOF(1)
Line Input #1, my_Word
Cells(i, 1) = my_Word
i = i + 1
Loop
Close
doc_Count = doc_Count + 1
my_Doc = Dir
Loop
MsgBox "共导入" & doc_Count & "个文件"
End Sub
Sub import_txt()
On Error Resume Next
Dim my_Link, my_Doc As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
.AllowMultiSelect = False
my_Link = .SelectedItems(1)
End With
Dim doc_Count As Single
doc_Count = 0
my_Doc = Dir(my_Link & "\" & "*.txt")
Do While Len(my_Doc) <> 0
Worksheets.Add
ActiveSheet.Name = my_Doc
Dim my_Word As String
Dim i As Single
i = 1
Open my_Link & "\" & my_Doc For Input As #1
Do While Not EOF(1)
Line Input #1, my_Word
Cells(i, 1) = my_Word
i = i + 1
Loop
Close
doc_Count = doc_Count + 1
my_Doc = Dir
Loop
MsgBox "共导入" & doc_Count & "个文件"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询