Input past end of file VBA excel

后端 未结 1 1542
北恋
北恋 2021-02-10 16:39

I am attempting to read text file (these files are produced by external program, that could not be tweeked) using the following macro.

    While Not EOF(int_cur         


        
相关标签:
1条回答
  • 2021-02-10 17:07

    May I interest you in a better way of reading text files in VBA?

    This will read the entire text file in ONE GO in an array and then close the file. This way you don't need to keep the file open at all times.

    Option Explicit
    
    Sub Sample()
        Dim MyData As String, strData() As String
        Dim i As Long
    
        '~~> Replace your file here
        Open "C:\MyFile.Txt" For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1
        strData() = Split(MyData, vbCrLf)
    
        '
        '~~> Now strData has all the data from the text file
        '
    
        For i = LBound(strData) To UBound(strData)
            Debug.Print strData(i)
            '
            '~~> What ever you want here
            '
        Next i
    End Sub
    
    0 讨论(0)
提交回复
热议问题