...表格如何使用vba控件将文本文件导入excel表格里
发布网友
发布时间:2024-10-10 14:26
我来回答
共1个回答
热心网友
时间:2024-10-31 14:14
建一个文件夹,把目标txt文件和excel文件放入其中,在excel的宏编辑器中写入以下代码,有些地方可根据你的实际情况做相应改动:
Sub import_from_txt()
Dim file_name As String, my_path As String
Dim lines, cols
Dim i As Integer, j As Integer, k As Integer, q As Integer
Application.ScreenUpdating = False
Worksheets(\"Sheet1\").Range(\"A1:Z65536\").ClearContents
my_path = ThisWorkbook.Path
file_name = \"test.txt\"
'读取文件
Open my_path & \"\\\" & file_name For Input As #1
lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
k = UBound(lines) + 1 '文件的行数
'遍历每一行
For i = 1 To k
cols = Split(lines(i - 1), \