【发布时间】:2016-05-31 08:36:48
【问题描述】:
早上好,
我尝试编写代码以: 1.打开一个txt。文件,其中包含文件列表 2.一个一个打开列表中的文件 3.读取每个文件中的内容,放入sheet中
我的代码在这里:
Private Sub Boutton_Importer_Click()
list_de_controle = "TEXT;" & listPath
Open listPath For Input As #1 'open the list
Do While Not EOF(1) 'read the list
Line Input #1, nom_de_Fich
ActiveCell = nom_de_Fich
ActiveCell.Offset(0, 1).Select
Open nom_de_Fich For Input As #2 'open a file in the list
Do While Not EOF(1) 'read the contents in the list
Line Input #2, contenu
ActiveCell = contenu
ActiveCell.Offset(0, 1).Select
Loop
Close #2
ActiveCell.Offset(1, 0).Select 'go to the line below
ActiveCell.End(xlToLeft).Select
Loop
Close #1
End Sub
您可能会发现 Do While 的两部分完全相同,但对于列表,第一部分运行良好。 第二个,对于文件中的内容,总是失败。 你能帮我检查一下吗? 提前谢谢!
【问题讨论】:
-
我忘记了一件事,列表中的第一个文件可以打开,所有内容都可以读入工作表,但下一个文件无法打开。所以我认为问题在于 EOF 无法确定它是否已经结束。