2013-03-13 2 views
2

줄 "Input # 1, Numbers (Row, Column)"은 런타임 버그 9를 제공합니다. 코드의 주석은 일어날 일을 설명합니다. 코드 아래Visual Basic Excel txt 입력 파일

Sub InputImage() 
'brings a text file to the workbook 

Dim Row As Long, Column As Long, Nrow As Long, Ncolumn As Long 

    Call MsgBox("Navigate to a folder that contains the file image.txt in the 00_18 folder") 
    ' The next statement will open a dialogue box that will let 
    ' the user navigate to any folder on their system 
    Call Application.GetOpenFilename 
Open "image.txt" For Input As #1 

Dim Buffer As String 
Line Input #1, Buffer 'read a whole line of characters from 
    'file #1 into the string variable buffer 

Line Input #1, Buffer 
Input #1, Buffer, Nrow 
Input #1, Buffer, Ncolumn 

Dim Numbers(1 To 4, 1 To 4) As Long 
For Row = 1 To Nrow 
    For Column = 1 To Ncolumn 
     Input #1, Numbers(Row, Column) 
    Next Column 
Next Row 

Close #1 

End Sub 
+0

오류가 발생했을 때 행과 열의 값은 무엇입니까? –

+0

오류 9 아래 첨자가 범위를 벗어남을 의미하며 텍스트 파일의 데이터가 아직 친구가 아닙니다 :-). image.txt 파일에서 읽고 자하는 데이터를 배열 번호에 게시 할 수 있다면 도움이 될 것입니다. – dee

+1

ADO를 사용하여 쿼리를 텍스트 파일로 읽을 수 있음을 알 수 있습니다. [MSDN Scripting Clinic : Text Files에 관한 많은 ADO] (http://msdn.microsoft.com/en-us/library/ms974559)를 참조하십시오. aspx) –

답변

0

봅니다 :

Option Base 1 
Sub InputImage() 
'brings a text file to the workbook 
    Dim j As Integer, i As Integer 
    Dim Row As Long, Column As Long, Nrow As Long, Ncolumn As Long 

    Call MsgBox("Navigate to a folder that contains the file image.txt in the 00_18 folder") 
    ' The next statement will open a dialogue box that will let 
    ' the user navigate to any folder on their system 
    Call Application.GetOpenFilename 


    Dim FileNum As Integer 
    Dim DataLine As String 

    FileNum = FreeFile() 
    Open "image.txt" For Input As #FileNum 


    j = 1 
    While Not EOF(FileNum) 
     Line Input #FileNum, DataLine ' read in data 1 line at a time 
     ' decide what to do with dataline, 
     ' depending on what processing you need to do for each case 
     'you can use split function to split DataLine with required delimiter which will return you an array. 
     a = Split(DataLine, vbTab) 

     For i = 1 To UBound(a) 
      Cells(j, i).Value = a(i) 
     Next 
     j = j + 1 
    Wend 


End Sub 

당신의 image.txt 가정하면 아래 이미지와 같다.

enter image description here

관련 문제