EXCEL如何批量导入TXT
发布网友
发布时间:2022-04-24 04:48
我来回答
共5个回答
热心网友
时间:2023-10-29 13:22
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之Sub FileProcess1() Dim FileFind As FileSearch
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path
FileStyle = "*.txt"
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之Sub FileProcess1() Dim FileFind As FileSearch
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path
FileStyle = "*.txt"
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
在E盘中有四个txt文件:
test1.txt , test2.txt , test3.txt , test4.txt
vba代码:
Sub test()
Dim filePath As String '路径
Dim fileName As String 'txt文件名
Dim txtStr 'txt内容,按照行读入
Dim txtIndex As Integer '第几个txt文件,此文件内容放在Excel的第txtTndex列
Dim lineinputIndex As Integer 'txt内容的第几行,放在Excel的第lineinputIndex+1行
filePath = "E:\" '按照自己的txt文件目录更改
fileName = Dir(filePath & "*.txt", vbDirectory)
Do While fileName <> ""
'MsgBox fileName
txtIndex = txtIndex + 1
lineinputIndex = 1
Sheet1.Cells(1, txtIndex) = fileName
Open filePath & fileName For Input As #1
Do While Not EOF(1)
lineinputIndex = lineinputIndex + 1
Line Input #1, txtStr
Sheet1.Cells(lineinputIndex, txtIndex) = txtStr
Loop
Close 1
fileName = Dir
Loop
End Sub
放入模块中,运行即可
热心网友
时间:2023-10-29 13:23
因为你的txt文件是以逗号分隔的,可以批量改扩展名为.CSV,就可以直接用EXCEL打开了,然后另存为xls格式。
热心网友
时间:2023-10-29 13:22
在E盘中有四个txt文件:
test1.txt , test2.txt , test3.txt , test4.txt
vba代码:
Sub test()
Dim filePath As String '路径
Dim fileName As String 'txt文件名
Dim txtStr 'txt内容,按照行读入
Dim txtIndex As Integer '第几个txt文件,此文件内容放在Excel的第txtTndex列
Dim lineinputIndex As Integer 'txt内容的第几行,放在Excel的第lineinputIndex+1行
filePath = "E:\" '按照自己的txt文件目录更改
fileName = Dir(filePath & "*.txt", vbDirectory)
Do While fileName <> ""
'MsgBox fileName
txtIndex = txtIndex + 1
lineinputIndex = 1
Sheet1.Cells(1, txtIndex) = fileName
Open filePath & fileName For Input As #1
Do While Not EOF(1)
lineinputIndex = lineinputIndex + 1
Line Input #1, txtStr
Sheet1.Cells(lineinputIndex, txtIndex) = txtStr
Loop
Close 1
fileName = Dir
Loop
End Sub
放入模块中,运行即可
热心网友
时间:2023-10-29 13:23
因为你的txt文件是以逗号分隔的,可以批量改扩展名为.CSV,就可以直接用EXCEL打开了,然后另存为xls格式。
热心网友
时间:2023-10-29 13:24
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码 代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之 SubFileProcess1() DimFileFindAsFileSearchDimfsAsVariantDimFilePathAsStringDimFileStyleAsString FilePath=ThisWorkbook.PathFileStyle="*.txt"WithApplication.FileSearch .LookIn=FilePath .Filename=FileStyle .Execute ForEachfsIn.FoundFiles SetXLSHEET=Workbooks.Open(fs) XLSHEET.SaveAsFilename:=Replace(fs,"txt","xls") XLSHEET.Save XLSHEET.Close NextfsEndWithEndSub
热心网友
时间:2023-10-29 13:24
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path ’路径
FileStyle = "*.txt" ‘该路径下的TXT文档
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之Sub FileProcess1() Dim FileFind As FileSearch
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path
FileStyle = "*.txt"
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
在E盘中有四个txt文件:
test1.txt , test2.txt , test3.txt , test4.txt
vba代码:
Sub test()
Dim filePath As String '路径
Dim fileName As String 'txt文件名
Dim txtStr 'txt内容,按照行读入
Dim txtIndex As Integer '第几个txt文件,此文件内容放在Excel的第txtTndex列
Dim lineinputIndex As Integer 'txt内容的第几行,放在Excel的第lineinputIndex+1行
filePath = "E:\" '按照自己的txt文件目录更改
fileName = Dir(filePath & "*.txt", vbDirectory)
Do While fileName <> ""
'MsgBox fileName
txtIndex = txtIndex + 1
lineinputIndex = 1
Sheet1.Cells(1, txtIndex) = fileName
Open filePath & fileName For Input As #1
Do While Not EOF(1)
lineinputIndex = lineinputIndex + 1
Line Input #1, txtStr
Sheet1.Cells(lineinputIndex, txtIndex) = txtStr
Loop
Close 1
fileName = Dir
Loop
End Sub
放入模块中,运行即可
热心网友
时间:2023-10-29 13:24
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码 代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之 SubFileProcess1() DimFileFindAsFileSearchDimfsAsVariantDimFilePathAsStringDimFileStyleAsString FilePath=ThisWorkbook.PathFileStyle="*.txt"WithApplication.FileSearch .LookIn=FilePath .Filename=FileStyle .Execute ForEachfsIn.FoundFiles SetXLSHEET=Workbooks.Open(fs) XLSHEET.SaveAsFilename:=Replace(fs,"txt","xls") XLSHEET.Save XLSHEET.Close NextfsEndWithEndSub
热心网友
时间:2023-10-29 13:23
因为你的txt文件是以逗号分隔的,可以批量改扩展名为.CSV,就可以直接用EXCEL打开了,然后另存为xls格式。
热心网友
时间:2023-10-29 13:24
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码 代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之 SubFileProcess1() DimFileFindAsFileSearchDimfsAsVariantDimFilePathAsStringDimFileStyleAsString FilePath=ThisWorkbook.PathFileStyle="*.txt"WithApplication.FileSearch .LookIn=FilePath .Filename=FileStyle .Execute ForEachfsIn.FoundFiles SetXLSHEET=Workbooks.Open(fs) XLSHEET.SaveAsFilename:=Replace(fs,"txt","xls") XLSHEET.Save XLSHEET.Close NextfsEndWithEndSub
热心网友
时间:2023-10-29 13:24
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path ’路径
FileStyle = "*.txt" ‘该路径下的TXT文档
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:24
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path ’路径
FileStyle = "*.txt" ‘该路径下的TXT文档
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之Sub FileProcess1() Dim FileFind As FileSearch
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path
FileStyle = "*.txt"
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub
热心网友
时间:2023-10-29 13:22
在E盘中有四个txt文件:
test1.txt , test2.txt , test3.txt , test4.txt
vba代码:
Sub test()
Dim filePath As String '路径
Dim fileName As String 'txt文件名
Dim txtStr 'txt内容,按照行读入
Dim txtIndex As Integer '第几个txt文件,此文件内容放在Excel的第txtTndex列
Dim lineinputIndex As Integer 'txt内容的第几行,放在Excel的第lineinputIndex+1行
filePath = "E:\" '按照自己的txt文件目录更改
fileName = Dir(filePath & "*.txt", vbDirectory)
Do While fileName <> ""
'MsgBox fileName
txtIndex = txtIndex + 1
lineinputIndex = 1
Sheet1.Cells(1, txtIndex) = fileName
Open filePath & fileName For Input As #1
Do While Not EOF(1)
lineinputIndex = lineinputIndex + 1
Line Input #1, txtStr
Sheet1.Cells(lineinputIndex, txtIndex) = txtStr
Loop
Close 1
fileName = Dir
Loop
End Sub
放入模块中,运行即可
热心网友
时间:2023-10-29 13:23
因为你的txt文件是以逗号分隔的,可以批量改扩展名为.CSV,就可以直接用EXCEL打开了,然后另存为xls格式。
热心网友
时间:2023-10-29 13:24
因为没有导入操作代码,以下代码是起到将文本文件另存为XLS文件代码 代码拷贝入文本文件夹中的一个新建EXCEL文件的模块中,执行之 SubFileProcess1() DimFileFindAsFileSearchDimfsAsVariantDimFilePathAsStringDimFileStyleAsString FilePath=ThisWorkbook.PathFileStyle="*.txt"WithApplication.FileSearch .LookIn=FilePath .Filename=FileStyle .Execute ForEachfsIn.FoundFiles SetXLSHEET=Workbooks.Open(fs) XLSHEET.SaveAsFilename:=Replace(fs,"txt","xls") XLSHEET.Save XLSHEET.Close NextfsEndWithEndSub
热心网友
时间:2023-10-29 13:24
Dim fs As Variant
Dim FilePath As String
Dim FileStyle As StringFilePath = ThisWorkbook.Path ’路径
FileStyle = "*.txt" ‘该路径下的TXT文档
With Application.FileSearch
.LookIn = FilePath
.Filename = FileStyle
.Execute
For Each fs In .FoundFiles
Set XLSHEET = Workbooks.Open(fs)
XLSHEET.SaveAs Filename:=Replace(fs, "txt", "xls")
XLSHEET.Save
XLSHEET.Close
Next fs
End With
End Sub