2016-05-31 31 views
2

早上好,VBA_using“线路输入”,但失败(错误62:输入过去文件的结尾)

我试图以写代码: 1.打开一个txt。文件,其中包含文件 2.打开列表中的一个接一个 3.read每个文件中的内容的文件的列表,并把它放在表

我的代码是在这里:

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的两部分完全相同,但列表中的第一部分运行良好。 而第二个,对于文件中的内容,总是失败。 你能帮我检查一下吗? 提前谢谢!

+0

我忘记了一些东西,在列表中的第一个文件可以打开,所有的内容可以以表来读取,但接下来的文件可以不会被打开。所以我认为问题在于EOF无法确定它是否已经达到最终结果。 – Hiddenllyy

回答

2

的问题是在这里:

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 

你通过和Line Input从文件#2告诉代码回路,但条件是基于到达文件的末尾文件#1

如果你还没有真正通过文件#1语句移动EOF(1)永远是真实的 - 这个循环将运行并不可避免地大卖文件#2结束,此时你会得到错误

文件的输入过去结束


解决您的问题:

尝试这样的事情,而不是:

Sub Foo() 

Dim textFile1 As Byte 
Dim textFile2 As Byte 
Dim tfArray1 As Variant 
Dim tfArray2 As Variant 

textFile1 = FreeFile 

Open listPath For Input As #textFile1 
    tfArray1 = Split(Input(LOF(textFile1), textFile1), vbCrLf) 
Close #textFile1 

For Each tfile In tfArray1 

    textFile2 = FreeFile 

    Open tfile For Input As #textFile2 
     tfArray2 = Split(Input(LOF(textFile2), textFile2), vbCrLf) 
    Close #textFile2 

    Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(UBound(tfArray2) + 1, 1).Value = _ 
     WorksheetFunction.Transpose(tfArray2) 

Next 

End Sub 
+0

非常感谢!我试图改变EOF(1)到EOF(2),并且它工作。但我不知道这是否会导致一些问题? – Hiddenllyy

+0

不应该这样做 - 但我更新了我的答案,以包含更好的方式来完成您试图实现的目标,所以如果您愿意,请尝试一下。不要忘记标记为答案,如果它帮助 –

+0

我看到了!我会试一试。当然,我会给出一个印记!非常感谢! – Hiddenllyy

相关问题